Results 1 to 14 of 14

Thread: randomize 1 to 80 numbers better than the Randomize function vb6

  1. #1

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    randomize 1 to 80 numbers better than the Randomize function vb6

    I have created a keno game that selects 20 numbers out of 80 using the Randomize function.
    The program also tracks which numbers have been selected the most.
    after running the app hundreds of thousands of times on different computers i have determined that the Randomize function is not truly random.
    for instance Runs: 219,098 Numbers 1 to 80
    these numbers are always at the the top of the list, but not always in the same exact order
    on different computers
    53 58054
    40 57993
    52 57957
    49 57948
    54 57909
    39 57883
    55 57864
    37 57860
    35 57847
    36 57823
    33 57735
    The numbers 70 to 80 are always at the bottom of the list
    eg
    77 51991
    78 51950
    80 51940
    70 51842
    71 51785
    79 51381
    72 51087
    73 50876
    74 50604
    76 50504
    75 50389
    how can i correct this ?
    using vb6
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: randomize 1 to 80 numbers better than the Randomize function vb6

    What's your seed? Most randomizers are not true random number generators, but pseudo ones. If you seed your function with your computer clock, which measures time in milliseconds, you probably won't see the same numbers repeating in the small amount of times you run the program (relatively).

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: randomize 1 to 80 numbers better than the Randomize function vb6

    Shuffle all the integers from 1 to 80 using the Knuth shuffle and then select the first 20.
    Doctor Ed

  4. #4

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: randomize 1 to 80 numbers better than the Randomize function vb6

    Don't know what seed is. Hired a coder to write the code. it looks like this:
    Const DRAWN_NUMBERS As Integer = 20
    For i = 1 To DRAWN_NUMBERS
    Randomize
    thisIndex = Int(((80 - i) * Rnd) + 1)
    DrawnNumber(i) = CandidateNumber(thisIndex)
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: randomize 1 to 80 numbers better than the Randomize function vb6

    The issue may well the fact that Randomize is being used inside the loop. Why is it that so many people don't seem to know how to do such a simple thing properly? Randomize is supposed to be used once and once only. It initialises the random number generator. You then call Rnd repeatedly and it will return the next number in the pseudo-random sequence. That sequence is plenty random enough for the vast majority of cases, if used properly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: randomize 1 to 80 numbers better than the Randomize function vb6

    Try building a form with a command button and a list box. Then apply this code:

    Code:
    Dim Nums() As Integer, Temp As Integer
    Dim RangeLimit As Integer, SwapValue As Integer
    Const PoolSize = 80, Draws = 20
    
    Private Sub Command1_Click()
    RangeLimit = PoolSize
    Do While RangeLimit > 1
        Do
            SwapCount = SwapCount + 1
            Temp = Int(Rnd * RangeLimit) + 1
            If Temp <> RangeLimit Then Exit Do
        Loop
        SwapValue = Nums(Temp)
        Nums(Temp) = Nums(RangeLimit)
        Nums(RangeLimit) = SwapValue
        RangeLimit = RangeLimit - 1
    Loop
    List1.Clear
    For I = 1 To Draws
        List1.AddItem Str$(Nums(I))
    Next
    End Sub
    
    Private Sub Form_Load()
    Randomize
    ReDim Nums(PoolSize)
    For I = 1 To PoolSize
        Nums(I) = I
    Next
    End Sub
    Doctor Ed

  7. #7

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: randomize 1 to 80 numbers better than the Randomize function vb6

    i noticed you put the randomize in the form load. my program can be ran thousands of times and each time it produces 20 random numbers.
    Would it not need to be called before each run ?
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: randomize 1 to 80 numbers better than the Randomize function vb6

    No, it should only be called once in total - unless you have specific requirements (such as generating lists of numbers that you can repeat later).

  9. #9

    Thread Starter
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: randomize 1 to 80 numbers better than the Randomize function vb6

    Quote Originally Posted by si_the_geek View Post
    No, it should only be called once in total - unless you have specific requirements (such as generating lists of numbers that you can repeat later).
    Don't understand
    app gets loaded
    Pressing the start button selects 20 random numbers
    Pressing start again the 20 numbers are erased and another 20 are randomly selected
    Repeat, repeat etc
    i noticed the randomize was inside a loop so i changed it to randomize before each startName:  HitNumbers.jpg
Views: 2289
Size:  89.6 KB
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: randomize 1 to 80 numbers better than the Randomize function vb6

    Rather than randomize before each start, just call it once in total (in Form_Load etc). The results are "more random" that way.

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: randomize 1 to 80 numbers better than the Randomize function vb6

    I'm no statistician but I think, simply put, it goes something like this:

    The VB Random Number Generator is actually a Pseudo Random Number Generator which is a Random distribution of 16,777,215 numbers. Initial seeding (Randomize) effectively 'points' the Generator at one of the numbers in the distribution from where it will start. The initial seed is derived from the system time (to 1mS). Calls to Rnd() select the next random number, the previous random number being the seed for the next. etc. Clearly, the more random the seed the more random the result.

    If you change the seed for the next number, by calling Randomize again, then the seed is less random; the time may not have moved on, given the speed of CPUs these days you could be in the same milli-second as you were before, so you would just start at the same point. Even if it has moved on you will be re-seeding 'sequentially increasing' rather than randomly, since, by definition, time goes forwards rather than backwards, instead of re-seeding with any of the 16,777,215 possible values, you will always be re-seeding with a number that was larger than, or equal to, the one before (going over midnight excepted). As your experience has demonstrated, this will affect the 'randomness' of the numbers returned.

    Setting the initial seed once for each execution of the Program (i.e. using Randomize once) ensures that the seed for each number is genuinely random throughout that execution.

    If you need more than 16,777,215 Random Numbers, in one execution, then you'll have to use a different algorithm (there's loads lying around the Internet) or hardware.

    (I suppose for 'random' you should read 'pseudo random')
    Last edited by Doogle; Nov 3rd, 2012 at 03:35 AM. Reason: Made a few corrections.

  12. #12
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: randomize 1 to 80 numbers better than the Randomize function vb6

    Quote Originally Posted by si_the_geek View Post
    Rather than randomize before each start, just call it once in total (in Form_Load etc). The results are "more random" that way.
    That's what I did with the code that I offered. To my knowledge, my code works and produces unbiased random selections.
    Doctor Ed

  13. #13
    New Member
    Join Date
    Sep 2021
    Posts
    1

    Re: randomize 1 to 80 numbers better than the Randomize function vb6

    Hi, in order to get a REAL random seed (based on milliseconds), just use this little trick:

    Code:
    <script runat="server" language="javascript">
        function getSeed() {var tmp=new Date().getTime()+'';return tmp.substr(tmp.length-9)}
    </script>
    
    <% randomize getSeed() %>
    This is because ASP Classic cannot read milliseconds, so if two users request a random number in the same second they would receive the same number (because of the same seed, based on time expressed in seconds). With Javascript run at server, the problem is elegantly solved.

  14. #14
    The Idiot
    Join Date
    Dec 2014
    Posts
    3,002

    Re: randomize 1 to 80 numbers better than the Randomize function vb6

    I think isnoend07 is done with this problem. not sure he cares 9 years later.
    also this is VB6 and we dont need to call java for this particular problem.
    we can use API calls or other means instead of VBA.Rnd

Posting Permissions

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



Click Here to Expand Forum to Full Width