Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
Random numbers
Name: RogueStar Date: November 2, 2003 at 15:56:59 Pacific OS: WinXP CPU/Ram: Duron 1.2 /384
Comment:
I'm new to C++ programming, and I've discovered the need for a random number generator. What would be the best way to go about this? In this case, I won't need a number more than 30 or so
Name: RogueStar Date: November 2, 2003 at 16:02:16 Pacific
Reply:
Sorry, I'm using VC++ 6, with out bothering with creating windows at this point.
0
Response Number 2
Name: Rolos Date: November 2, 2003 at 18:41:34 Pacific
Reply:
You would need to use the modulus operator for random numbers in C++. So you would do something like:
rand() % 30;
That would give you a random number from 0 to 30. Hope this helps.
- Rolos
0
Response Number 3
Name: JasonR Date: November 3, 2003 at 04:44:01 Pacific
Reply:
Small correction to last post
rand() % 30
will give numbers between 0 and 29
0
Response Number 4
Name: Rolos Date: November 3, 2003 at 16:29:53 Pacific
Reply:
Jason: Thanks for catching that, you are absolutely right.
Rogue Star: What you would do to accomodate for this is to just add a one to the expression as such:
int someVariable; someVariable = ((rand() % 50) + 1);
You may also want to do some exception handling if you want to implement 0's that are returned from the function call into your program. But it is dependant on your implementation and I won't go into it.
I hope that I have answered your question and good luck with your project/assignment.
- Rolos
0
Response Number 5
Name: RogueStar Date: November 4, 2003 at 13:26:04 Pacific
Reply:
Thanks for the help, gang. Dearly appreciated. That should be enough for me to proceed. Currently working on a text adventure for a sort of "final project" as I work my way through to the end of the book. Anybody want to have a look after it's done, I'll make it available for testing
0
Response Number 6
Name: Iuri Cernov Date: November 6, 2003 at 13:23:40 Pacific
Reply:
Before use the rand() function, you need this function:
// in DOS or Unix void randomize(void) { srand((unsigned)time(NULL)); }
// in Unix void randomize(void) { FILE *fl = fopen('/dev/random', 'rb'); unsigned short i; if (fl == NULL) return; fread(&i, 2, 1, fl); srand(i); // fclose(fl) // do not need! }
Summary: Is it possible to get an array of unique random numbers, for example all numbers between 1 and 20 in a random order but only once, in an Excel spreadsheet or as a table in Access? Do I need to use Vis...
Summary: I have to write a program that has an inputed number of "rolls of the dice" and I am to output how many times say, six of a kind or two triples occur. I know how to do random numbers, but they have to...
Summary: I'm looking for a way to large random numbers beyond the limit of rand(). I have an idea on how I can do this but was wondering if C++ by chance had anything to help me out built in or if someone how ...