#### Archived

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

# What exactly does this code mean?

This topic is 5581 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]

##### Share on other sites
What about quantum theory? Isn''t it ''random''? If the universe is not deterministic, then there is some randomness, somewhere.

Cédric

##### Share on other sites
What makes you think the universe might not be deterministic? Quanta do what they do for a reason. We may not ever understand it, but they follow RULES, and they cannot ever deviate from the rules. Therefore, simply because we can''t quantify the dynamics of their behavior, it doesn''t mean they behave randomly.

Later,
ZE.

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

##### Share on other sites
I dunno. I feel the same as you do, but from what I''ve read, Einstein believed in a deterministic universe all his life (hence the famous quote: "God does not play with dices"), but apparently, quantum theory proves that even if we had complete knowledge of the universe, we could not predict it (or is it that we can''t get complete knowledge?)

I probably got it all backward. If you know more than I do, please, slap me on the wrists for being stupid

Cédric

##### Share on other sites
I don''t know enough on the subject to make further comments, but he seems pretty adamant that his service offers true random numbers: http://www.random.org/essay.html

dMDI

##### Share on other sites
Even if true randomness does not exist, we''ll never know because of the Heisenberg principle.

To generate good random number you use multiple psuedo-random sources and use a couple of the lsb from each one (they are the most random). For example, if you periodicly execute rtdsc, it''s unlikely that the last two digits will always be the same.
And if the motherboard has a thermister (thing that reads the temperature of the CPU) on it, you can use the last one or two lsb of that as random numbers too.

##### Share on other sites
MKH: You meant the MSB, right ?

Numerical Recipes, among other sources, explicitely warn against unsing the low-order bits of linear-congruential pseudorandom number generators (i.e. most rand() functions), since they exhibit comparatively less random patterns.

That''s why the code posted by the OP is preferable to using a modulo operation (which mostly works from the low-order bits).

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

##### Share on other sites
I''m pretty sure that the lsb is what should be used in MKH''s post. There might be a problem with the lsb in numerical pseudo-random number generators, but if you take the last digit of precision in a numerical thermometer, it will be much more random than the first one.

Cédric

##### Share on other sites
Isn''t true randomness the source for creating real artificial intelligence?

That''s why I think our brain has kind of like a source for true random events.

##### Share on other sites
Some more clarifications...

Almost all of random number generators found in language libraries are Linear Congruential Generators. This is a specific algorithm for generating random numbers. While it is fast, the numbers generated are sometimes not random enough for particular purposes. When looked at in a certain higher dimensional space, the numbers it generates tend to cluster about certain planes. To make matters even worse, in such generators, the low order bits are often much less random than their high order bits. When you use a modulo operation, you are effectively only taking the lower order bits to construct your random number.

Most of the time, this generator is sufficiently random... if you notice your application is starting to have predictive behaviour, you have two options:
1) don''t use modulus when creating random numbers
2) get a better generator - there are other generators that are much more random and higher performance also

Radioactive decay is not random in that that if you sit and watch a sample and plot number of decays versus time, the number of decays per unit time will exponentially drop off in a very well known manner. However, if you watch any individual atom, there is absolutely no way to tell when it will decay. There is no observation you can make that will tell you when it will decay - it is truely random, there is nothing deterministic about it. This is a big discussion in physics, whether or not there are "hidden variables" that we are unable to detect that could make this behaviour predictable - but this is considered to be far-out physics, more in the realm of philosophy by most physicists.

##### Share on other sites
The Linux kernel (and I''m sure others) use times of various interrupts (don''t know precisely which, probably hd, keyboard, mouse) to add into a sort of random pool to seed generators. Quite random I''d think.

##### Share on other sites
You''re arguing against yourself when you say that there may be hidden factors that affect the way decay occurs. The fact is, NOTHING is "truly random" until there is no REASON for it to do what it does. REASON is NOT subjective. Just because we are unable to predict something doesn''t mean it''s random.

Look at 9-11. Did you wake up that morning thinking, "Hey, I bet there''s gonna be a terrorist attack today!"? Did the thought of thousands dying even cross your mind? Probably not, but it happened, all the trillions of factors came together, and a tragedy was created. You had no information that indicated to you such a thing would soon happen, but it did nonetheless, for a number of solid, immutable, and very calculated reasons.

There is no such thing as luck.

Peace,
ZE.

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

##### Share on other sites
quote:
Original post by Birdman
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.

Well, as far as I know, you can''t determine e.g. an electron''s exact position; you have only a kind of "probability cloud" where it might be with what probability. Since this is a random effect at the very basics, we certainly do have a random effect in nature.

_________________________
"Reality is merely an illusion, albeit a very persistent one." (Albert Einstein)
My Homepage

##### Share on other sites
randomZ: The fact that it's a probability cloud doesn't mean it's random, it simply means we can't know for sure where it is at this present moment. The reason we can't know the exact position is because the fact of observing it changes its state. And the fact that the whole universe is somewhat interacting with this single atom makes it even more "random LIKE".

Some people think brain electric signal are TOTAL random. That's wrong, they LOOK like random and would be good pseudo-random number generator for a game. But if you create a super-complex system as complex as a brain, these value will no longer be random.

The point here is trying to emulate randomness, not have a real one. Reading a quantum state from an atom would be a real damn good pseudo-random number generator since it will take a system as complex as the current universe until it get obsolete and I truly think it's going to take a while

EDIT : Typo

[edited by - Coincoin on September 2, 2002 3:46:15 AM]

##### Share on other sites
quote:
Original post by ZealousElixir
What makes you think the universe might not be deterministic?
What makes you think the universe is deterministic? There''s currently no way to prove one or another.
quote:
Quanta do what they do for a reason. We may not ever understand it, but they follow RULES, and they cannot ever deviate from the rules.
You''re simply stating your belief as a fact, which is quite silly. Reminds me of religious arguments. I would''ve let it slip by as your way of saying things, if it were only 1 comment.. But you''ve been touting the same thing in 3 posts now. You''re doing nothing more than claiming that your religion is the only correct one.

##### Share on other sites
"There is no such thing as luck."

ZealousElixir, I''m sorry but there IS luck. If we all follow your definitions about everything having it''s purpose, we''d all pretty much end up being fucked-up. And nobody cares about randomness of cosmic noise anyway. One day you''ll die and nobody will even remember you saying anything. No Luck? For sure.

• ### Forum Statistics

• Total Topics
628702
• Total Posts
2984295

• 22
• 10
• 9
• 13
• 13