Results 1 to 6 of 6

Thread: Bad Random Function?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715

    Bad Random Function?

    Hi,

    I have a function that generates a random 6-Digit Number, so there should be at least 6^10 possible combinations. However I ran the function 1000 times and I got the same numbers 3 or 4 times. Here is my code:

    VB Code:
    1. <%
    2.     Function RandomPW()
    3.         For X = 1 To 6
    4.             Randomize
    5.             strPW = strPW & CHR(Int((9 * Rnd) + 48))
    6.         Next
    7.         RandomPW = strPW
    8.     End Function
    9.    
    10.     for i = 0 to 1000
    11.         response.write RandomPW() & "<br>"
    12.     next
    13. %>

    After I got the result, I put it into Excel and sorted it. Attached is the list that I was returned, after I sorted it in Excel.

    Am I doing something wrong as far as generating random numbers?

    thanks,

    Dimava
    Attached Files Attached Files
    NXSupport - Your one-stop source for computer help

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715

    Re: Bad Random Function?

    Upon doing more anaylsis, it appears that the random list repeats itself every after every 128 generations
    NXSupport - Your one-stop source for computer help

  3. #3
    Junior Member
    Join Date
    Oct 2004
    Location
    world
    Posts
    19

    Re: Bad Random Function?

    In this line check the 9 it is constant try make it variable
    strPW = strPW & CHR(Int((9 * Rnd) + 48))
    try this
    strPW = strPW & CHR(Int((x * Rnd) + 48))

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

    Re: Bad Random Function?

    You should not be calling Randomize repeatedly, once is enough - and will make the data appear to be more 'random', as each value will be based on the previous value rather than being completely new.

    Also, as you are only allowing 9 possible values in 6 characters, repetition is going to be more frequent than if you had a larger range of values (such as 26 values in 10 chars).

  5. #5
    Addicted Member
    Join Date
    Oct 2003
    Location
    Newark-on-trent, Nottingham
    Posts
    243

    Re: Bad Random Function?

    Is there i way of generating a random number and them not generating this number again? So say i wanted to generate 10 random numbers between 1 and 100 how do i stop it repeated the same number twice?

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

    Re: Bad Random Function?

    To do that there are two main options, as the amount of possible values (100) and the amount to be selected (10) are both so small, either method should be ok. Note that unless the amount of possible values is large the second method is likely to be more efficient in most cases.

    1) Create the random numbers and store them in an array, when you create a new one check that it hasn't been used before; if it has, generate another number. eg:
    VB Code:
    1. Dim aNumbers(9) As Integer
    2. Dim iCount As Integer
    3. Dim iRnd As Integer
    4. Dim iCheck As Integer
    5. Dim bIsValid As Boolean
    6.  
    7.   For iCount = 0 To 9
    8.     Do
    9.             'pick the random number
    10.       iRnd = Int((Rnd(1) * 100) + 1)
    11.      
    12.             'see if it has been used before
    13.       bIsValid = True
    14.       For iCheck = 0 To (iCount - 1)
    15.         If aNumbers(iCheck) = iRnd Then
    16.             'it has - so try again
    17.           bIsValid = False
    18.           Exit For
    19.         End If
    20.       Next iCheck
    21.     Loop Until bIsValid = True
    22.             'this number is unused, so use it
    23.     aNumbers(iCount) = iRnd
    24.   Next iCount

    2) Don't actually use random numbers, you create a list (array or collection) of the valid numbers, and pick from random positions (removing items as they are used).
    VB Code:
    1. 'set up list of valid numbers
    2. Dim cNumbers As Collection
    3. Dim iCount As Integer
    4.   Set cNumbers = New Collection
    5.   For iCount = 1 To 100
    6.     cNumbers.Add iCount
    7.   Next iCount
    8.  
    9. Dim aNumbers(9) As Integer
    10. Dim iRndPos As Integer
    11.   For iCount = 0 To 9
    12.             'pick the random position
    13.     iRndPos = (Rnd(1) * cNumbers.Count) + 1
    14.             'get the item from that position (our "random" number)
    15.     aNumbers(iCount) = cNumbers.Item(iRndPos)
    16.             'remove the item from the list
    17.     cNumbers.Remove iRndPos
    18.   Next iCount

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