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.
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.
Yes. We covered this not too long ago in random number. You should also read Prelude's article on using rand().
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
accually there is a way using this:
i think this is correct but anyway i' ll check this tommorow for sure...Code:int x; x = low_limit + (rand()%(hight_limit - low_limit));
Randomness is impossible. This is how you generate a pseudorandom integer between one and ten.
This assigns x to a pseudorandom integer between zero and nine and adds one to it.Code:srand(time(NULL)); int x = (rand() % 10) +1;
Last edited by Babkockdood; 09-02-2010 at 03:29 PM.
here is my code:
this prints numbers between [10 - 20]...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; }
i hope i helped!
Last edited by brack; 09-02-2010 at 04:02 PM.
It is correct, but may introduce bias. You should likewise read the links that I provided.Originally Posted by brack
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.Originally Posted by Babkockdood
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
"Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin."Code:Random number: 7 Random number: 14 Random number: 28 Random number: 35 Random number: 42
~John von Neumann
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...
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
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
And your point is?Originally Posted by Babkockdood
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.
Last edited by laserlight; 09-03-2010 at 04:47 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Last edited by msh; 09-04-2010 at 07:09 AM.