Results 1 to 2 of 2

Thread: Probability & RNDF

  1. #1
    Member marcuslee's Avatar
    Join Date
    Sep 2008
    Location
    Kansas, USA
    Age
    42
    Posts
    222
    Rep Power
    38

    Probability & RND

    Concerning ˇmpáct!, I've been thinking about how to have events happen based upon a certain probability. For example, there is a 1% chance that something will happen. Or, there is a 30% chance that something else will happen. How can I capture this in code?

    I've thought of the following:

    x = RND(1, 100)
    IF x <= PercentVariable then <<do something>>
    
    So, if there is a 1% chance that something will happen, x will have to be 1. If there is a 30% chance, x will have to be 30 or less. Does this capture the probability I am looking for?




    Mark
    Last edited by marcuslee; 17-11-2010 at 10:06. Reason: changed topic title

  2. #2
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152
    I ran the following thinBasic program, and the output was, 29,994,029.

    That seems pretty good, since, perfection would be, 30,000,000.

    (Apparently, the generated distribution is very close to uniform, so that the statement, "If Rnd <= 0.3", should be true, almost exactly 30% of the time.)


    Dan

    Uses "console"
    
    Global i, c As Long
    
    Function TBMain()
    
    ' Seed the random number generator using the number of elapsed seconds since midnight.
    Randomize
    ' (To instead make a reproducible sequence, use a constant seed, like, "Randomize(2)".)
    ' (Note that, "Randomize()", produces the same sequence as, "Randomize(0)".)
    
    c = 0
    
    ' Generate 100,000,000 extended precision random values, and determine how many are less than or equal to 0.3.
    
    For i = 1 To 100000000
    If Rnd <= 0.3 Then c += 1
    Next
    
    Console_WriteLine(c)
    
    WaitKey
    End Function
    
    Last edited by danbaron; 18-11-2010 at 11:30.
    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

Similar Threads

  1. sugestion for rnd & rndf
    By sandyrepope in forum Fixed or cleared errors in help material
    Replies: 1
    Last Post: 21-05-2008, 06:35

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •