Is there any simple way to generate random numbers in C++?
I see all these generators like Mersenne(sp) Twister, but all I need is something to let me generate a number between 1 and 20.
Thanks.
Is there any simple way to generate random numbers in C++?
I see all these generators like Mersenne(sp) Twister, but all I need is something to let me generate a number between 1 and 20.
Thanks.
Code:#include<iostream> #include<ctime> int main() { srand(time(0)); std::cout<<(rand()%20)+1; std::cin.get(); return 0; }
Excellent, thats exactly what I needed.
One more question, is there a way to bind that number to a variable?
Sure -
Code:#include<iostream> #include<ctime> int main() { srand(time(0)); int x = rand()%20+1; std::cout<<x; std::cin.get(); return 0; }
Beware rand(). It's deceptively simple, and tricky to get right. In fact, once you use random numbers for more than a quick selection of a few numbers, you'll begin to see why everyone prefers to find a better generator.
My best code is written with the delete key.
and what exactly would be a 'better generator'?
(i've been looking :-[ lol)
//edit - like the link your provided? lol
btw, the code you have at the bottom of your page works wonderfully
Last edited by willc0de4food; 08-19-2005 at 10:19 PM.
Registered Linux User #380033. Be counted: http://counter.li.org
The 'easy' code probably works fine in most cases but if I'm trying to pick a random student from a pool of 80, how 'non-random' is it?
I'm going to try to test it, look for a new thread.
-JM
>and what exactly would be a 'better generator'?
This is arguably the best generator for random numbers presently, but this is surprisingly good as well, despite the bad reputation that simpler linear congruential generators (like rand() is usually implemented as) have.
>The 'easy' code probably works fine in most cases
It depends heavily on the quality of rand(). If it's decent then the easy code probably will work just fine for simpler cases.
>how 'non-random' is it?
Once again, it depends on the quality of the generator and how you use it. If you need guaranteed good randomness then rand() is a tossup because it isn't required to be very good.
My best code is written with the delete key.
>and what exactly would be a 'better generator'?
And, you can't forget LavaRnd
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
- http://en.wikipedia.org/wiki/Mersenne_twisterUnlike Blum Blum Shub, the algorithm in its native form is not suitable for cryptography. For many other applications, however, it is fast becoming the random number generator of choice.
anyone have sample code for blum blum shub?
hahaha...thats really funny to say.
Registered Linux User #380033. Be counted: http://counter.li.org
another way:
Code:int x = rand% 21; // instead of 20+1 put 21 dont be "#$ lol
That does not work (aside from the missing paren). The range of your function is [0, 20], while the desired range for the function is [1, 20].
And, it uses the "less random" low order bits.
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
Then that generates values in the range [1, 21]. rand() % 20 generates values in the range [0, 19]; noting that % has higher precedence than +, we see that (rand() % 20) + 1 == rand() % 20 + 1 has values in [1, 20].
But nevertheless, you should see Prelude's tutorial (linked above) for the way you should use rand, to avoid using only the less random low-order bits.
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.