473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Random Numbers and Chance

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?

Mar 16 '06 #1
8 1202
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.

Mar 16 '06 #2
> 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...

Mar 16 '06 #3
> 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]

Mar 16 '06 #4

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

Mar 16 '06 #5
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...

Mar 16 '06 #6
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.

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

Mar 17 '06 #8
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.

Mar 17 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Hans A | last post by:
I have a textfile "textfile.txt" containing a list of words. There is one word on each line. I want to pick two random lines from this textfile, and I have tried to do something like: //Loading...
4
by: james blair | last post by:
Hi I am generating a random number using random.randint(1,10000000000) Whats the possibility that the numbers generated will be same when generated by 100 users at the same time? Whats the best...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
15
by: Steven Macintyre | last post by:
Hi all, I need to retrieve an integer from within a range ... this works ... below is my out puts ... it just does not seem so random ... Is there perhaps a suggestion out there to create a...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
34
by: Johannes Baagoe | last post by:
About Math.random(), ECMA 262 just says "Returns a number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform...
8
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0;...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.