I talked about this a few months ago in Visual Basic Rnd() function tricks and traps, where I discovered that I wasn't getting a full 32bits of randomness out of VB's Rnd() function in my VBScript/Visual Basic pages. At the time, I thought it was simply due to Rnd() returning a 32bit IEEE floating point value which really only has 30bits of data.
In reality, the situation is even worse:
PRNG (Pseudo Random Number Generator) By Michael Meelis
Microsoft Visual Basic: Pseudo Random Number Generator
Using and Creating Cryptographic-Quality Random Numbers By Jon Callas
True random number generators
VB's Rnd() function only provides 24bits of randomness. After the 2^24th sample, it starts repeating. (See INFO: How Visual Basic Generates Pseudo-Random Numbers for the RND Function.)
Randomize() just contributes to the problem. See An Examination of Visual Basic's Random Number Generation By Mark Hutchinson and notice that calling Randomize() is based on the time of the day and only provides around 2^16 different starting points within the 2^24 stream. So if you're calling Randomize() before every Rnd(), you're only getting 16bits of randomness. (It's actually worse, because you'll get identical starting points at the same time of day.)
Showing posts with label VisualBasic. Show all posts
Showing posts with label VisualBasic. Show all posts
Friday, November 25, 2005
Tuesday, August 30, 2005
Visual Basic Rnd() function tricks and traps
Found this while examining some code results this week.
One of our bits of code attempts to generate a negative 32bit integer to be used for a random ID. The code is pretty standard for Visual Basic, and is used just about everywhere:
The problem with this code is that the end result is always divisible by 4. So instead of getting ~2,147,483,647 unique values, we're only getting around 536,870,911. Needless to say, that tends to raise a bit of concern about the RNG in Visual Basic.
But, if you do some digging, it becomes pretty apparent why this happens. The Rnd() function returns a value of type "single", which is a 32-bit IEEE floating point value. (One bit is used for the sign, 8 bits for the exponent, and 23 bits for the mantissa.) The value returned is always >=0 and <1, which is only about 30bits worth of randomness.
So, when you try to eek out a full 31 or 32 bits worth of randomness, you simply don't have the bits to do it. There are ways to combine two calls to the Rnd() function to get more bits, but I need to do some research rather then posting a broken method of doing this.
One of our bits of code attempts to generate a negative 32bit integer to be used for a random ID. The code is pretty standard for Visual Basic, and is used just about everywhere:
'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
tmpUserID = Int((-1 - -2147483647 + 1) * Rnd + -2147483647)The problem with this code is that the end result is always divisible by 4. So instead of getting ~2,147,483,647 unique values, we're only getting around 536,870,911. Needless to say, that tends to raise a bit of concern about the RNG in Visual Basic.
But, if you do some digging, it becomes pretty apparent why this happens. The Rnd() function returns a value of type "single", which is a 32-bit IEEE floating point value. (One bit is used for the sign, 8 bits for the exponent, and 23 bits for the mantissa.) The value returned is always >=0 and <1, which is only about 30bits worth of randomness.
So, when you try to eek out a full 31 or 32 bits worth of randomness, you simply don't have the bits to do it. There are ways to combine two calls to the Rnd() function to get more bits, but I need to do some research rather then posting a broken method of doing this.
Labels:
2005,
VisualBasic
Friday, April 04, 2003
Web Spidering in Visual Basic
Internet Transfer Control ActiveX control sounds like what I'm looking for (trying to find out how to read the contents of a URL into a string variable to be processed.
google search
VBIP - control is only available in VB/VS v5 and v6 (Pro and Enterprise editions)
google search
VBIP - control is only available in VB/VS v5 and v6 (Pro and Enterprise editions)
Labels:
2003,
VisualBasic
Subscribe to:
Posts (Atom)