-
Random string function... not random
hey
i am using the below function to generate a random string of x amount of charactors:
Code:
Function RandomString(ByVal mask As String) As String
Dim i As Integer
Dim acode As Integer
Dim options As String
Dim char As String
' initialize result with proper lenght
RandomString = mask
For i = 1 To Len(mask)
' get the character
char = Mid$(mask, i, 1)
Select Case char
Case "?"
char = Chr$(1 + Rnd * 127)
options = ""
Case "#"
options = "0123456789"
Case "A"
options = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Case "N"
options = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0" _
& "123456789"
Case "H"
options = "0123456789ABCDEF"
Case Else
options = ""
End Select
If Len(options) Then
char = Mid$(options & Right$(options, 1), 1 + Int(Rnd * Len(options) _
), 1)
End If
Mid(RandomString, i, 1) = char
Next
End Function
problem is every time i call the function it produce the same output each time.. not random at all
-
Re: Random string function... not random
Wow, get to answer this one twice today.
In your form load event, ensure you use Randomize. Suggest adding this in Form_Load: Randomize Timer
The Randomize method seeds the random generator with the value passed, in this case the VB variable Timer.
Edited: Just a minute, do you mean you get the same results every single time? Or do you get the same results each time you start the project?
-
Re: Random string function... not random
What value are you inputting because when I do MsgBox RandomString("#ANH") it gives me a different result each time.
-
Re: Random string function... not random
MsgBox "IS this random???!" & RandomString("#ANH")
each time i run i get same result..
tried randomizing the timer also.
-
Re: Random string function... not random
Are you expecting the "IS this random???!" part to change? It doesn't, but the rest of it does.
-
Re: Random string function... not random
Quote:
Originally Posted by LaVolpe
Wow, get to answer this one twice today.
In your form load event, ensure you use Randomize. Suggest adding this in Form_Load: Randomize Timer
The Randomize method seeds the random generator with the value passed, in this case the VB variable Timer.
Edited: Just a minute, do you mean you get the same results every single time? Or do you get the same results each time you start the project?
Fox, I don't think the Randomize Timer is needed anymore. Just Randomize by itself in the Form Load will work to generate a different sequence.
-
Re: Random string function... not random
Probably not, per MSDN docs, if I recall, Randomize without a parameter will use the system clock to seed the generator.
-
Re: Random string function... not random
Brinx
I'm not sure what you meant by:
Quote:
tried randomizing the timer also.
but try putting the randomize command here:
Code:
Function RandomString(ByVal mask As String) As String
Dim i As Integer
Dim acode As Integer
Dim options As String
Dim char As String
Randomize
' initialize result with proper lenght
RandomString = mask
For i = 1 To Len(mask)
' get the character
char = Mid$(mask, i, 1)
Select Case char
Case "?"
char = Chr$(1 + Rnd * 127)
options = ""
Case "#"
options = "0123456789"
Case "A"
options = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Case "N"
options = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0" _
& "123456789"
Case "H"
options = "0123456789ABCDEF"
Case Else
options = ""
End Select
If Len(options) Then
char = Mid$(options & Right$(options, 1), 1 + Int(Rnd * Len(options) _
), 1)
End If
Mid(RandomString, i, 1) = char
Next
End Function
-
Re: Random string function... not random
The Randomize statement should only be used once and when the program starts like in Form_Load() as LaVolpe said.
-
Re: Random string function... not random
Really why? It can be used that way to repeat patterns. So why not insert it where he can see it in his code and understand what we're saying.
Here's the MSDN Link http://msdn.microsoft.com/en-us/libr...ek(VS.85).aspx
-
Re: Random string function... not random
Quote:
Originally Posted by technorobbo
Well that link is for .Net but in any case it has been shown that using Randomize more than once actually makes for a less random output and there's a thread somewhere that proves it.
-
Re: Random string function... not random
Quote:
Originally Posted by MartinLiss
Well that link is for .Net but in any case it has been shown that using Randomize more than once actually makes for a less random output and there's a thread somewhere that proves it.
It may be for .Net my VB5 doc is identical. As I always say Random is a misnomer. It should be called AlmostUnpredictable. And less Random would be an oxymoron. More predictable makes more sense but that would kind of depend on the application.
-
Re: Random string function... not random
Quote:
Originally Posted by MartinLiss
Well that link is for .Net but in any case it has been shown that using Randomize more than once actually makes for a less random output and there's a thread somewhere that proves it.
No actually its VBScript.
-
Re: Random string function... not random
Quote:
Originally Posted by Atheist
No actually its VBScript.
Okay but it doesn't really matter since it doesn't support either side of the argument. I'll find the thread I was talking about when I have the time.
-
Re: Random string function... not random
From My Database... :D
Code:
Private Sub Command1_Click()
'~~ For example generate a string of 10 chars
MsgBox GenerateRandomString(10)
End Sub
Private Function GenerateRandomString(ByVal lngLength As Long) As String
Dim iChr As Integer, c As Long, strResult As String, iAsc As String
Randomize Timer
For c = 1 To lngLength
'~~> Randomly decide on ASCII chars to be used
iAsc = Int(3 * Rnd + 1)
'~~> Randomly pick a char from the random set
Select Case iAsc
Case 1
iChr = Int((Asc("Z") - Asc("A") + 1) * Rnd + Asc("A"))
Case 2
iChr = Int((Asc("z") - Asc("a") + 1) * Rnd + Asc("a"))
Case 3
iChr = Int((Asc("9") - Asc("0") + 1) * Rnd + Asc("0"))
End Select
strResult = strResult & Chr(iChr)
Next c
GenerateRandomString = strResult
End Function
Another one from the same database
Code:
Private Sub Command1_Click()
'~~ For example generate a string of 10 chars
MsgBox GenerateRandomString(10)
End Sub
Function GenerateRandomString(ByRef length As Integer) As String
Randomize
Dim allowableChars As String
allowableChars = "abcdefghijklmnopqrstuvwxyz0123456789"
Dim i As Integer
For i = 1 To length
GenerateRandomString = GenerateRandomString & _
Mid$(allowableChars, Int(Rnd() * Len(allowableChars) + 1), 1)
Next
End Function
-
Re: Random string function... not random
Quote:
Originally Posted by MartinLiss
Okay but it doesn't really matter since it doesn't support either side of the argument. I'll find the thread I was talking about when I have the time.
I'm betting it's this one:
Proper use of the Randomize Statement
As demonstrated in that thread, calling Randomize only once results in a nice, smooth distribution of random numbers. Calling Randomize inside the loop results in a more spiky distribution.
-
Re: Random string function... not random
Quote:
Originally Posted by Ellis Dee
I'm betting it's this one:
Proper use of the Randomize Statement
As demonstrated in that thread, calling Randomize only once results in a nice, smooth distribution of random numbers. Calling Randomize inside the loop results in a more spiky distribution.
Yes, thank you!
-
Re: Random string function... not random
I think the point of that article is for generating Gaussian Normal Disributions. Hence the even peaks. Personally i would prefer something closer to a white noise pattern. Erratic distribution
-
Re: Random string function... not random
I don't follow. Let's say we're simulating dice. You'd rather some of the numbers come up more frequently than others than to have a more even distribution? Isn't that effectively using loaded dice?
-
Re: Random string function... not random
The frequency shouldnt be predictable. It should be chaotic.The way I read the MS algorithm there's a built in cycle of 2^24. Randomize should shake it up as long as it's not in a fixed loop.
-
Re: Random string function... not random
An even distribution doesn't make it predictable. It just means that no one number has a higher probability of being generated than another. Think of a single die (dice). Don't you want a 6 to roll once every 6 times on average?
-
Re: Random string function... not random
Quote:
Originally Posted by technorobbo
The frequency shouldnt be predictable. It should be chaotic.The way I read the MS algorithm there's a built in cycle of 2^24. Randomize should shake it up as long as it's not in a fixed loop.
I don't understand, but I'd like to. What, exactly, do you mean by "frequency" here?
-
Re: Random string function... not random
Dice are chaotic. PRNG's repeat and for games the repetition isn't that vital. For encryption it may be because it is predictable and you can synchronize 2 systems.
Frequency would mean how often they repeat and if they do repeat can they be hacked and synced by spotting the pattern.
BTW MS's algotithm (in C) is this (vb can't do plicated cause it doesnt have the unsigned long integer):
Code:
#include "stdafx.h"
int main(int argc, char* argv[])
{
unsigned long rndVal;
rndVal = 0x50000L;
int i;
float rndFloat;
for (i=0;i<10;i++)
{
rndVal = (rndVal * 0x43fd43fdL + 0xc39ec3L) & 0xffffffL;
rndFloat = (float)rndVal / (float)16777216.0;
printf("Value is %.15f\n",rndFloat);
}
return 0;
}
-
Re: Random string function... not random
Quote:
Originally Posted by technorobbo
Dice are chaotic.....
I may be misunderstanding you but dice are not chaotic. Given enough rolls they are highly predictable in that if I rolled a die 6000 times I'm sure that I'd get 1000 6's or at least pretty close to that.
-
Re: Random string function... not random
Remember what Mark Twain said about statistics. Dice are truly unpredicatble to much input and one little output. But the RND() algorithm is a shuffle. Once the seed is given the numbers come out in a predetermined pattern. Reseeding in an unplanned manner disrupts the sequence. Given its a large pattern but not large enough to avoid detection.
-
Re: Random string function... not random
I'd feel better about calling Randomize after every user interaction (keystroke or mouseclick) than I would about putting it inside the processing loop.