Random numbers

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
 
C

Chris Dunaway

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)
 
?

=?Windows-1252?Q?Jos=E9_Manuel_Ag=FCero?=

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
|
|
 
?

=?Windows-1252?Q?Jos=E9_Manuel_Ag=FCero?=

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
|
|
 
H

Herfried K. Wagner [MVP]

* "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>
 
C

Chris Dunaway

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.
 
M

Mick Doherty

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.
 
B

Bart Jacobs

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top