Hi.
Im looking for a simpley way to pick randomly one of thease numbers:
1-2-3-4.
I have searched on google, but finds nothing but how to get a randomnumber between 0 and some stupid max-number!
Hi.
Im looking for a simpley way to pick randomly one of thease numbers:
1-2-3-4.
I have searched on google, but finds nothing but how to get a randomnumber between 0 and some stupid max-number!
http://livebad.com/nuka
Da-Nuka
rand()%X (where X is a number) gives you a number between 0 and X-1. That's why we add the +1, so you get between 1 and X.Code:int somevariable = (rand()%4 + 1)
You could have always just read the FAQ entry on the subject.
[edit]
On an aside, did you never think to just simply add one to the number, since you know it starts at zero, and you want it to start at one? I hope you don't have to ask again when you want to generate a number starting at 2. Or 3. Or again at 4...
[/edit]
Quzah.
Last edited by quzah; 12-25-2004 at 04:09 PM.
Hope is the first step on the road to disappointment.
To answer your next question.. you probably forgot to seed your random number generator.
This is to be done only once.Code:srand(unsigned(time( ));
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel
To answer your next next question, you'll need a set of parenthesis around that unsigned, like this for it to actually work:
Oh, and of course you'll actually have to provide a value to the time function. So you really end up with something like this:Code:srand( (unsigned) time( ) );
Or, you could have just read the FAQ as I suggested, which covers srand.Code:srand( (unsigned) time( 0 ) );
Quzah.
Hope is the first step on the road to disappointment.
Oh course since this is c++ you could be using the proper type of casting:
Code:srand( static_cast<unsigned>( time(0) ) );
Then obviously you don't know what a max number is *cough* 4 is your max number for the sequent 1-2-3-4 */cough*. It's not brain surgery...and as Quzah said, the FAQ is god...Originally Posted by Da-Nuka
And you can't forget that time_t isn't guaranteed to be convertible to unsigned int:
Code:unsigned int random_seed() { unsigned int seed(0); time_t now = time(0); unsigned char *p = reinterpret_cast<unsigned char *>(&now); for (size_t i = 0; i < sizeof now; i++) seed = seed * (numeric_limits<unsigned char>::max() + 2U) + p[i]; return seed; } ... srand(random_seed());
My best code is written with the delete key.
Im not that noob
Just lazy!
All I needed was the function name... (first post)
And I feel I gotta defend my self a little:
I wasnt talking about 4 as the max number, if I had found ways to do that, the problem had been solved.
The maxnumber I allways bumbed into was RAND_MAX.
A example:
http://www.phim.unibe.ch/comp_doc/c_...IONS/rand.html
Last edited by Da-Nuka; 12-26-2004 at 03:25 AM.
http://livebad.com/nuka
Da-Nuka
>Just lazy!
That's a bad kind of lazy.
>I wasnt talking about 4 as the max number, if I had found ways to do that, the problem had been solved.
I don't understand. How is "get a randomnumber between 0 and some stupid max-number!" different from what you want? Unless you want a random permutation, what you've been finding is a viable solution. Give us more detail as to what you're trying to do.
My best code is written with the delete key.
We're not here to do work for you. We're here to help you with problems. If you know how to solve the problem and are just too lazy to do it yourself, you'll quickly find the same attitude towards helping you.Just lazy!