I've looked at this post
Random Number
and I checked preludes random number generator, but can't you make it shift to a new number every half or whole milisecond?
It would be better since it is, well, really slow for a game of dice.
This is a discussion on random number per milisecond within the C++ Programming forums, part of the General Programming Boards category; I've looked at this post http://cboard.cprogramming.com/showthread.php?t=53242 and I checked preludes random number generator, but can't you make it shift to ...
I've looked at this post
Random Number
and I checked preludes random number generator, but can't you make it shift to a new number every half or whole milisecond?
It would be better since it is, well, really slow for a game of dice.
Last edited by Xarr; 06-05-2004 at 11:00 AM.
Yeah, stop calling
srand( time(0) );
each time you want a new rand() value
You only need to call this ONCE per program run.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
every call to rand() will give you another number in the sequence. The time is only a factor when seeding the generator.
Edit: Dang beaten
Oh yeah almost forgot
How would I put the random number into a variable instead of outputting it?
The class std isn't defined in the code...
std is the namespace.
to put it in a variable you would just use it like any other return value from a function.
int x = rand();
but it isn't the rand() function i'm talking about it's the one prelude wrote. The 5th reply here: Random Number
there (edit: she) defines the implementation thing and the fucntion random().
What I would like to know is (apart from how to change it's shifting speed): how to save the number that the for loop makes into an variable "x" instead of cout'ing it
Last edited by Xarr; 06-05-2004 at 02:23 PM.
Its still a function. It follows the exact same rules as any other return value.
BTW Prelude is a she.
For dice, you'd use:Code:int x; std::srand(time_seed()); for (int i = 0; i < 10; i++) { x = static_cast<int>(random() * 10);
This will give an int between 0 and 5, representing one die.Code:int x; std::srand(time_seed()); for (int i = 0; i < 10; i++) { x = static_cast<int>(random() * 6);
>This will give an int between 0 and 5, representing one die.
A six sided die is the most common, but certainly not the only kind available.If I were to write a simple die thrower then I would include the number of sides as an argument:
Code:class diemension_error: public invalid_argument { public: diemension_error(const string& msg) : invalid_argument(msg) {} }; int die_toss( int sides ) { if (sides < 4) { throw diemension_error("3-D die only please"); // ;-) } return static_cast<int>(random() * sides); }
My best code is written with the delete key.
>class diemension_error: public invalid_argument {
DIEmension ... that's a good one Prelude, haha.![]()
That's some neat code. I'm going to try running it. Is invalid_argument in the std library?
>Is invalid_argument in the std library?
It is indeed. Be sure to include <stdexcept> though.
My best code is written with the delete key.