# Thread: Random numbers works in one program, not in another

1. (int)((double)rand() / ((double)RAND_MAX + 1) * N)

I don't even really understand what that means, but I am afraid to ask because I will be told to search for it on google instead of getting an explanation and I will get flamed for all eternity and excommunicated by the persons on the board.

2. I seem to remember Prelude giving an explanation on this. I didn't fully understand it at the time but she usually knows what she is talking about. It was on this board so you may find it by searching.

3. Well I am okay with what i have. By the way is there a way to clear the context of a txt file.

I thought fout.clear("Generated.txt"); would do it, but it doesnt

4. Originally posted by minesweeper
I seem to remember Prelude giving an explanation on this. I didn't fully understand it at the time but she usually knows what she is talking about. It was on this board so you may find it by searching.
What it seems to be saying is that most implementations of rand() dont offer a great amount of randomness for the low order bits and that you are advised to use higher order bits as they are more reliable.....so all the code
Code:
`(int)((double)rand() / ((double)RAND_MAX + 1) * N)`
is doing is converting the return to a double (adding trailing zeros)...Then once you have limited it to the range you want, you cast it back to an int (this discards the trailing decimal places)...therefore the low order bits of your expression have been discarded and you are left with what should be a better random number sequence......

5. Okay that actually does shed some light on to that...I've never heard of low order or high order bits before though. High order bits are found with doubles because they have more decimal places? and an int has low order bits because it has none?

6. >I seem to remember Prelude giving an explanation on this.
Basically, I said that the high order bits of most random number generators tend to be more random than the low order bits, so for more randomized sequences, division by RAND_MAX is better. For more information I'll direct you to Knuth vol.2, page 13.

Now, for most applications, using the low order bits will work just fine provided the range is wide enough to give a decent dispersion. So there is really nothing wrong with using

r = rand % N;

if one is willing to accept less than optimal pseudo-random numbers for that particular implementation. I will warn you now, if any part of this or previous explanations from me confuse you then following up with Knuth will help you little. The Art of Computer Programming series is not for the faint hearted even for looking up and understanding on little tidbit of information.

-Prelude

7. Knuth? The Arm of Computer Programming? What is this? A book series? Or is it an ebook or something.

8. So how do I get from SkinColor = rand()%5; to using
(int)((double)rand() / ((double)RAND_MAX + 1) * N).

SkinColor = ¿@!****ing g-â-+¼61

9. >Knuth? The Arm of Computer Programming? What is this? A book series? Or is it an ebook or something.
The Art of Computer Programming

>So how do I get from SkinColor = rand()%5; to using
>(int)((double)rand() / ((double)RAND_MAX + 1) * N).
SkinColor = (int)((double)rand() / (double)RAND_MAX * 5);

-Prelude

10. Do you recommend The Art of Computer Programming and The Elegant Universe?

I recommend Chromosome 6 by Robin Cook, although that has nothing to do with programming.

11. >Do you recommend The Art of Computer Programming
Yes. I've found that between the three of those books, most programming questions can be answered.

> and The Elegant Universe?
Off Topic. Check the GD board for my opinion on this book.

-Prelude

Is there some way you can do random numbers between some positive number and another positive number (or negative for that matter)

for example x = rand() %10 to 20

Or is that just being silly?
I don't have time right now to check if this is answered because I'm about to go out the door, but try something like this

Code:
`x = rand()%(highnum-lownum) + lownum`
I think thats right.

Edit: Sorry... I had run a search and forgot that it was a search and started reading topics.