Random numbers

  • Thread starter Thread starter GB
  • Start date Start date
G

GB

How can I create a random number in a set range without using the follwoing
code:

Randomize
Return cint(int((5*rnd()) + 0)

I am looking for something more random than the above as the result seem to
be predictable for small ranges.

Thanks,
Gary
 
How can I create a random number in a set range without using the follwoing
code:

Dim iRandomNumber As Integer

'Initialize the random number generator with a seed based on the system
'clock
Dim rnd As New Random(Environment.TickCount)

'Gets a random number from 1 to 9999
iRandomNumber = rnd.Next(1,10000)
 
Hello, GB:

You have complete information in the System.Random class topic, including where to look for a stronger random number generator and how to easily generate a number within a range:
http://msdn.microsoft.com/library/?...lrfSystemEnvironmentClassTopic.asp?frame=true

Regards.


"GB" <[email protected]> escribió en el mensaje | How can I create a random number in a set range without using the follwoing
| code:
|
| Randomize
| Return cint(int((5*rnd()) + 0)
|
| I am looking for something more random than the above as the result seem to
| be predictable for small ranges.
|
| Thanks,
| Gary
|
|
 
Hello, GB:

You have also complete information in the System.Random class topic (note this is not the same as Rnd), including where to look for a stronger random number generator and how to easily generate a number within a range:
http://msdn.microsoft.com/library/?...lrfSystemEnvironmentClassTopic.asp?frame=true

Regards.


"GB" <[email protected]> escribió en el mensaje | How can I create a random number in a set range without using the follwoing
| code:
|
| Randomize
| Return cint(int((5*rnd()) + 0)
|
| I am looking for something more random than the above as the result seem to
| be predictable for small ranges.
|
| Thanks,
| Gary
|
|
 
* "GB said:
How can I create a random number in a set range without using the follwoing
code:

Randomize
Return cint(int((5*rnd()) + 0)

I am looking for something more random than the above as the result seem to
be predictable for small ranges.

Have a look at the 'Random' class and its methods. Nevertheless, I
don't understand why the numbers generated by the pseudo-random number
generator should be easily predictable.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
 
don't understand why the numbers generated by the pseudo-random number
generator should be easily predictable.

Perhaps he was starting with the same seed each time.
 
Perhaps he was starting with the same seed each time.
Nope, he called Randomize first, unless of course his clock has stopped, in
which case he'll have the same problem with Random.
 
The Randomize command initializes the random number generator with the
current system time. The problem is that the resolution (i.e. the
precision or granularity) of the system clock is limited. If you run the
code below in a tight loop, no clock ticks will have a chance to occur
between every two iterations.

The solution is to randomize just once at the start of the application.
 
Back
Top