# Thread: Reliable Random Number Program

1. ## Reliable Random Number Program

This is something that always bothered me in C; random numbers are really cumbersome in it. Is there a way to generate random numbers within a certain range, as in a random number between 1 and 10? I think it'd be a useful thing to know.

2. Yes. We covered this not too long ago in random number. You should also read Prelude's article on using rand().

3. accually there is a way using this:

Code:
```int x;
x = low_limit + (rand()%(hight_limit - low_limit));```
i think this is correct but anyway i' ll check this tommorow for sure...

4. Randomness is impossible. This is how you generate a pseudorandom integer between one and ten.

Code:
```srand(time(NULL));
int x = (rand() % 10) +1;```
This assigns x to a pseudorandom integer between zero and nine and adds one to it.

5. here is my code:
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void){
int r, i;
float x;

printf("Type a number: \n");
scanf("%d", &r);
srand(r);
for(i=0; i<50; i++){
x = 10 + (rand() % (20-10)); // x = low_limit + rand() % (hight_limit - low_limit));
printf("x: %.0f \n", x);
}
return 0;
}```
this prints numbers between [10 - 20]...
i hope i helped!

6. Originally Posted by brack
i think this is correct but anyway i' ll check this tommorow for sure...
It is correct, but may introduce bias. You should likewise read the links that I provided.

Originally Posted by Babkockdood
Randomness is impossible.
That depends on what you define as "randomness". User Name:'s post #13 in the thread that I linked to provides an example of a possible source of randomness. Your example is another example of what brack described.

7. Originally Posted by laserlight
It is correct, but may introduce bias. You should likewise read the links that I provided.
I took a look at them, but i found them somehow difficult because i am still learning C...
so i tried so give an example...i hope i helped and not make somebody nervous...

8. Originally Posted by laserlight
That depends on what you define as "randomness". User Name:'s post #13 in the thread that I linked to provides an example of a possible source of randomness. Your example is another example of what brack described.
Psuedorandom number generation in computers takes input from the system that isn't very predictable and puts it through a huge mathematical equation to generate a number that appears random.

Randomness in general, is impossible. Think about it. If you roll dice, the result depends on how you rolled it and what face pointed up when you rolled it. If you flip a coin, the result depends on how you flipped it and what face pointed up originally. If you pick something out of a hat, the result depends on where everything is in the hat and where you decide to pick it.

I once ran the snippet of code I posted with a simple printf statement. I ran the program once every second and the output looked like this.

Code:
```Random number: 7
Random number: 14
Random number: 28
Random number: 35
Random number: 42```
"Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin."
~John von Neumann

9. My opinion: I am a programmer...i mean i want to be someday and i write down C code, so when i hear "randomness" i think "numbers" without a specific sequence...in fact i don' t care what "randomness" means in the "real word"...Oh, also the next words are the same for me randomness ~ psuedorandom...

10. Originally Posted by Babkockdood
Randomness in general, is impossible.
Well. Depends on the source. From what I understand, atmospheric noise, radioactive decay and
background radiation are very good sources of randomness.

NaE, tho.

11. Originally Posted by Babkockdood
Psuedorandom number generation in computers takes input from the system that isn't very predictable and puts it through a huge mathematical equation to generate a number that appears random.
EDIT: frankly, you are not clear. The random aspect is the "input from the system that isn't very predictable". The "huge mathematical equation" (which is not necessarily "huge") sounds like you are indeed talking about a pseudorandom number generator, where the random input is converted to a number that is used merely as a seed. But if the random input is itself converted directly to a random number, then we call the generator a random number generator, not a pseudorandom number generator. You can disagree on philosophical grounds, but practicality trumps philosophy here.

Originally Posted by Babkockdood
Randomness in general, is impossible. Think about it. If you roll dice, the result depends on how you rolled it and what face pointed up when you rolled it. If you flip a coin, the result depends on how you flipped it and what face pointed up originally. If you pick something out of a hat, the result depends on where everything is in the hat and where you decide to pick it.
This is why I say that it depends on what you define as "randomness". One can use philosophical notions of randomness, or one can use notions of randomness that have practical applications, e.g., statistical randomness, unpredictability, and lack of reliable reproduction. For the latter, the roll of a fair die might be regarded as random, even if one might argue that it is deterministic. Likewise, one might make use of the theoretical randomness of radioactive decay, even if "God does not play dice with the universe".

Originally Posted by Babkockdood
I once ran the snippet of code I posted with a simple printf statement. I ran the program once every second and the output looked like this.
And your point is?

EDIT:
Hold on, you "ran the program once every second"? That is absurd: to make good use of a pseudorandom number generator, you should be printing out a sequence from the same seed, not continually reseeding at one second intervals and taking the first number in each sequence. Your failure to use a PRNG correctly calls into question your own skills, not the quality of the PRNG.

12. If anyone is interested, I've crated a DLL based on Prelude's article. You can download the source here.

I've tested it on Win7 and WinXp. Any feedback would be greatly appreciated.

Popular pages Recent additions