#### Archived

This topic is now archived and is closed to further replies.

# What exactly does this code mean?

This topic is 5802 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Here I have code for creating ''truly random numbers''. (int)((double)rand() / ((double)RAND_MAX + 1) * 10); The result from rand() is cast into a double, and then divided by the result from rand_max plus 1 which is also cast into a double, and then multiplied by the maximum number 10, and then that is finally cast into an integer? I asked someone about this code and they said it is for generating truly random numbers because they are low order bits or something. If I interpreted this code wrong tell me, if you have any opinions about random numbers tell me.

##### Share on other sites
It's not more random than anything else. What it does is to generate a "random" number between 0.0-1.0, and then multiplying it by the max value, so you get an integer value between 0 and 10 (or whatever max you want)

EDIT: Hmm, due to the division by RAND_MAX + 1, it can never reach 10 in this case, the maximum here would be 9.

[edited by - CWizard on August 31, 2002 10:11:54 AM]

##### Share on other sites
That still generates pseudo-random numbers. However, because you don''t use modulo to cap the range you aren''t scewing the distribution (that''s what they were trying to get across by explaining about the ''low order bits'').

##### Share on other sites
How does not using modulo help prevent the ''screwing of the distribution''

What do you by mean pseudo random?

##### Share on other sites
Psuedo Random Numbers ->

There is no such thing is a random number generated by a computer... Any equation used to generate a random number isn''t random.

The only things that are truly random, are natrual things, like the decay of uranium, sunspots, etc.

##### Share on other sites
And acutally, sunspots and uranium decay aren''t random either. The sunspots have scientific principles for why they form where they form, just like everything does. Roll and ball down a hill and you can calculate just where it will hit and bounce. There are no ''random'' numbers, since where would one come from? There must always be a seed value, and if there is, its not random.

Look! There! Up in the sky! It''s Birdman! Da-du-du-daaaaah!

##### Share on other sites
If you want very random numbers then use some sort of device to measure the electrical noise of a resistor (I read about it in a book somewhere). I heard of a group that were using a lava lamp to generate random numbers, but lava lamps actually are rather repetitive (the goo goes up and down, up and down, up and down...) so they don''t make good random number generators.

--Thomas McCorkell

Just what is Karma? Is it a way to rate people? A way of assigning privilege levels? Or is karma just an anti-spam system?

##### Share on other sites
On the subject of extreme randomness, it really amounts to whether or not you believe in determinism, but I remember reading about a PC card that generated 'truly random numbers'. They had observed the output, and had not found any repeatable pattern after x numbers.

And, yes, using the modulo doesn't really screw up the distribution because RAND_MAX is quite large. It's only screwed if your max is quite big comparatively to RAND_MAX. For example,

max = 10000
RAND_MAX = 65536

If you use the modulo, the numbers between 0 and 5536 are favored over the other numbers. However, in this case, using floating-points will generate other kinds of errors because of rounding issues when they are converted back to int. Some ints will be favored over others.

The point is, if you have a random-number generator with x discrete values, you can't get a uniform distribution for y discrete values unless y is a factor of x.

EDIT: Precision: in the example above, you could discard the numbers between 60000 and 65536, and ask for another number from rand() to get a uniform distribution, but that's not one random number.

Cédric

[edited by - cedricl on August 31, 2002 1:08:38 PM]

[edited by - cedricl on August 31, 2002 1:11:34 PM]

##### Share on other sites
www.random.org generates truly random numbers, as it records the background radiation from space, which can not be predicated.

dMDI

##### Share on other sites
As was said above, there is no such thing as "true randomness." Background noise in space may be unpredictable with current technology, because of all the factors involved, but every event stems from the fundamental rules of the universe. There is no chance that a random element will be added in, because everything occurs within the bounds of natural law, which is constant and immutable, even if we can't "predict" it.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//

[edited by - zealouselixir on August 31, 2002 2:45:39 PM]

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631790
• Total Posts
3002367
×