views:

128

answers:

3

What's the fastest way to randomly generate numbers, either randomly or simulating random? I don't really need a true random number generator, it would be acceptable to simulate random. I tried other random simulation methods but none were faster than this.

Here's the fastest way I have now:

Private myRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
Private sub Test1
     Dim F as integer = myRandom(0, 203)
End Sub
+2  A: 

Do you truly need the fastest method? What are the minimum constraints on the qualities of it?

I suspect that the built-in random number generator, Random, although not necessarily the fastest, has a reasonable tradeoff between "minimal standards" and speed.

If not, here's some info on some other random number generators, some of which may be faster, some of which may be worse. There's going to be a tradeoff between quality and speed, so I doubt you would want the fastest one.

http://www.google.ca/search?source=ig&hl=en&rlz=1R2ADSA_enCA338&q=FAST+pseudo+random+number+generator&meta=lr%3D&aq=f&oq=

`

Larry Watanabe
+3  A: 

The fastest way I can think of to get random numbers is to generate a large array of random numbers in advance that you cycle through repeatedly. This could also yield very poor quality results unless the size of the array were enormous. Quality would also be affected by how you generate the numbers.

Andy West
+1  A: 

Here are three ways to do it, in order of speed (19 ms for a million iterations, in my test):

dim rand as new random(0)

i = rand.next

A little slower is a call to the xkcd function (24 ms):

function random as integer
random = 4 ' chosen by fair dice roll
end function

And a little slower yet is the floating point VB function (67 ms):

x = rnd(x)

Each of these is faster even than taking the last few bits of the system clock ticks. The function overhead seems to be enough to justify using the builtin random functions. For example, if you generate a floating point random number using almost any algorithm, it will end up being slower than the rnd vb function. Same for integer and the random class.

xpda