# Random Numbers and Chance

Discussion in 'ASP .Net' started by Chu, Mar 16, 2006.

1. ### ChuGuest

I'm developing a game - in the game, the character has a 1 in 1000
chance of finding a special weapon. The pseudo code idea is this:

get a random number from 1 to 1000

If random number is "1" then the user found a special weapon.

My question is - will using .NET's buildin Random class really give me
a true "1 in 1000" chance like I'd like as specified above? Are there
any other ways that would be more precise?

Chu, Mar 16, 2006

2. ### KBuserGuest

the Random number generator uses the system clock to seed the
generation... So if you have 10 calculations in less than a clock tick
you will get the same 10 results.

KBuser, Mar 16, 2006

3. ### Paul HendersonGuest

> My question is - will using .NET's buildin Random class really give me
> a true "1 in 1000" chance like I'd like as specified above? Are there
> any other ways that would be more precise?

Basically, no random number generator on a conventional computer is
"truly" random. The Random class uses an algorithm based on a seed
(starting) value and modifying this to obtain the next "random" number.
Hence, there is a predictable relation between one random number and
the next, but this is not perceptible in conventional usage.

The distribution of numbers throughout a range (in this case 1 - 1000)
will be even on average, so you will get true 1/1000 chance. There is
not really a more precise way, but as it is random, a slight variation
is inherent, whatever method you use...

Paul Henderson, Mar 16, 2006
4. ### Paul HendersonGuest

> the Random number generator uses the system clock to seed the
> generation... So if you have 10 calculations in less than a clock tick
> you will get the same 10 results.

The Random number generator is *seeded* based on the system clock, but
it should only be re-seeded when the program is started. Each number
generated is a function of the previous, and so calling many times per
unit time will not yield the same result. [Note I say "unit time"; a
"tick" is not really correct as it refers to a single cycle, in which
you could by definition call the RNG at most once]

Paul Henderson, Mar 16, 2006
5. ### KBuserGuest

Paul Henderson wrote:
> > the Random number generator uses the system clock to seed the
> > generation... So if you have 10 calculations in less than a clock tick
> > you will get the same 10 results.

>
> The Random number generator is *seeded* based on the system clock, but
> it should only be re-seeded when the program is started. Each number
> generated is a function of the previous, and so calling many times per
> unit time will not yield the same result. [Note I say "unit time"; a
> "tick" is not really correct as it refers to a single cycle, in which
> you could by definition call the RNG at most once]

Interesting. Many months ago I was doing something similar to what he
was doing, and I was getting the same number repeatedly output for a
split second then it would change numbers and repeat that one about as
many times. It was at that point somebody had explained to me the
seeding issue, and told me that the number didn't get reseeded. I just
created a simple program to test it, and it does appear to reseed it.
I'm going to look for my old source code and see if I can't figure that
out....

KBuser, Mar 16, 2006
6. ### KBuserGuest

I just wrote a program to randomly generate a number 0-99 50,000 times,
it then counts the occurence of each number... Heres a sample of the
output:
The number 0 occurs 458 times.
The number 1 occurs 496 times.
The number 2 occurs 504 times.
The number 3 occurs 485 times.
The number 4 occurs 498 times.
The number 5 occurs 511 times.
The number 6 occurs 473 times.
The number 7 occurs 462 times.
The number 8 occurs 496 times.
The number 9 occurs 503 times.
The number 10 occurs 514 times.
The number 11 occurs 477 times.
The number 12 occurs 477 times.
The number 13 occurs 498 times.
The number 14 occurs 533 times.
The number 15 occurs 514 times.
The number 16 occurs 515 times.
The number 17 occurs 492 times.
The number 18 occurs 506 times.
The number 19 occurs 534 times.
The number 20 occurs 526 times.

Not too shabby...

KBuser, Mar 16, 2006
7. ### ChuGuest

I plan to get around the seed problem by using a static class that
seeds it the first time using the system clock. Every time after that I
call the getRandom method it will be called on the same random object
that was seeded when the program first started. This way, I shouldn't
have to worry about getting duplicate random numbers sequentially that
occur in the same clock tick.

Chu, Mar 17, 2006
8. ### KBuserGuest

Judging from my program's output, I don't think you have to... If you
want the source just let me know.

KBuser, Mar 17, 2006
9. ### ChuGuest

In your program did you recreate a new random object or did you reuse
the same one for all your number generation? If you use the same one,
then you are basically doing the same as me by keeping the object
static.

Chu, Mar 17, 2006