I made a program that simulates the roll of a six sided dice. I had my program return a random number between 1 and 6. The weird thing is I get 4 like 6 out of 10 times each time I run the program. So is the random number genarator really random or is there a way to make numbers more random?

All 2 Replies

>So is the random number genarator really random
No. The random number generator creates pseudorandom numbers that just look good to a point. Depending on the quality of the generator that point will be quick to arive or not.

>The weird thing is I get 4 like 6 out of 10 times each time I run the program.
That sounds fairly normal. A completely random distribution would have each number equally likely to come up. But pseudorandom numbers aren't quite that good and you can expect slightly skewed results. The best solution is to provide as random of a seed as you can, or replace the random number generator with something better.

Most of the computerized random number generators that I have seen use the last few digits of the time field (milliseconds?) as the seed. Since the start time is going to be random this starts of the random number generator at a random spot in its sequence.

Computer random number generators usually use a formula to derive one number from the previous and eventually cycle through all possible numbers between zero and a fairly large number (usually tens of thousands) but jumping all over the place within the number range during the process. As you usually only want a small number (say up to 100) returned when these tens of thousands of numbers are scaled down they give the appearance of a random sequence of numbers.

The important thing to remember with random number generators is that if you start with the same seed then you will always get the same "random" sequence. That is why you need to use a random number to seed the generator to start with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.