|
-
May 6th, 2013, 05:55 PM
#6
Re: Confused on Random
The problem is that you either never called Randomize, or are calling it in the wrong place. The purpose of the Randomize function is to see the random number generator (typically with the current system time down to the second). If you don't call Randomize then you will be getting a canned set of numbers. If you call Randomize too often then you will get the exact same sequence of numbers over and over until the system time increases, at which point you will get a new sequence of numbers for the next second, and so on. For that reason, you MUST call Randomize before using Rnd, but you must be calling it only once per run of the program.
The Random object is better, as there is no need to call Randomize (which should do nothing, but seems to cause trouble, though I haven't looked too carefully at it). However, the same problem exists. If you create new Random objects more than once per second you will get the same sequence from them. Therefore, you want to create only one Random object at form scope and use that wherever you need a random number.
However, I'm also curious as to why you would find something like this:
CInt(Int((3 - 1 + 1) * Rnd() + 1))
easier than just calling the .Next method of the Random object in any circumstance?
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|