Random Numbers and Chance

C

Chu

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

KBuser

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

Paul Henderson

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

Paul Henderson

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]
 
K

KBuser

Paul said:
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....
 
K

KBuser

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

Chu

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

KBuser

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

Chu

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top