Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Number Function (Rnd) Not Random!!!

Status
Not open for further replies.

DeanWilliams

Programmer
Feb 26, 2003
236
GB
Hi,

Sorry if this is a bit long.

I have written a function that returns a random string of a random length. I used the random (Rnd) function to create these strings:

The user specifies the shortest length they want the string to be and the function then determines the length at random between that number and 20. Then each character is randomly chosen from upper/lower case and number characters.

I would have thought the results would be completely random, but have found that I get the EXACT same strings sometimes when I use it!!! [sadeyes]

I have been using it to create PID's for Access security, so it is not ideal if these strings can be determined again at a later stage.

I hope that makes sense, but does anyone know why this happens? And does anyone know how to generate totally random numbers?

This is the main part of the code:
Code:
[COLOR=blue]
Function CreatePID(intLBound As Integer, strUser As String, _
                            TypePID As PIDType, Optional strLogFile As String) As String
On Error GoTo Err_CreatePID

    Dim strResult As String
    Dim intLength As Integer
    Dim strCharSet As String
    Dim intChar As Integer
    Dim X As Integer
    Dim strType As String
    
    strCharSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    
    intLength = Int((20 - intLBound + 1) * Rnd + intLBound)

    For X = 1 To intLength
        intChar = Int((Len(strCharSet) - 1 + 1) * Rnd + 1)
        strResult = strResult & Mid$(strCharSet, intChar, 1)
    Next
    
    Debug.Print strUser, strResult
    ' ....
    ' ....
[/color]




Dean.
 
Hi!

Have you tried using the Randomize statement prior to using the Rnd function?

Roy-Vidar
 
If you're not supplying a seed, this will occurs.

Look at this from Help.

Syntax

Randomize [number]

The optional number argument is a Variant or any valid numeric expression.

Remarks

Randomize uses number to initialize the Rnd function's random-number generator, giving it a new seed value. If you omit number, the value returned by the system timer is used as the new seed value.

If Randomize is not used, the Rnd function (with no arguments) uses the same number as a seed the first time it is called, and thereafter uses the last generated number as a seed value.

Note To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.
 
Hi,

Thanks guys. I did read that in the help file, but did not really understand exactly what it meant. Thanks I will try that.

Dean :)
 
Dean,

Disclaimer: I am not an expert.

Per VB Help:
If Randomize is not used, the Rnd function (with no arguments) uses the same number as a seed the first time it is called, and thereafter uses the last generated number as a seed value.

So, you might want to add Randomize:

intLength = Int((20 - intLBound + 1) * Rnd + intLBound)

For X = 1 To intLength
Randomize
intChar = Int((Len(strCharSet) - 1 + 1) * Rnd + 1)
strResult = strResult & Mid$(strCharSet, intChar, 1)
Next

HTH,
Bob [morning]
 
Good Grief!

I'm glad Dean's question was answered...

There weren't any other posts when I first looked.

Bob
 
ooooh, wot a thread - brought back some wonderful memories of sitting writing lottery programs for my MSC Information Systems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top