I read the tutorial on rand, but it didnt make much sense
(sorry RoD, thanks anyway) can someone explain it to me
Printable View
I read the tutorial on rand, but it didnt make much sense
(sorry RoD, thanks anyway) can someone explain it to me
Generate random number.
Returns a pseudo-random number in the range from 0 to RAND_MAX constant. This is generated by an algorithm that returns a series of non-related numbers each time is called. This algorithm should be initialized to different starting points using function srand to generate more realistic random numbers.
RAND_MAX is a constant defined in stdlib.h. Its default value is implementation defined.
Parameters.
(none)
Return Value.
An integer value between 0 and RAND_MAX.
Code:/* rand example */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
/* initialize random generator */
srand ( time(NULL) );
/* generate some random numbers */
printf ("A number between 0 and RAND_MAX (%d): %d\n", RAND_MAX, rand());
printf ("A number between 0 and 99: %d\n", rand()%100);
printf ("A number between 20 and 29: %d\n", rand()%10+20);
return 0;
}
thus rand()%25 would be a random number between 0 and 24, both included.Code:Output:
A number between 0 and RAND_MAX (32767): 30159
A number between 0 and 99: 72
A number between 20 and 29: 23
A good way to generate almost-true random numbers is to initialize the random algorithm using srand with the current time in seconds as parameter, as obtained from time function included in <time.h>.
And, generally, a good way to get an integer random number between a range is to perform a module (%) operation on a result provided by rand():
Looks for posts on this I had one called "randomizing" I asked the same question. I'll be nice and briefly explain it
Here:
Code:#include <cstdlib>
#include <iostream>
#include <ctime> //need this
using namespace std;
int main(int argc, char *argv[])
{
srand((unsigned int)time(0) ); //seeds the generator
int number = rand()%10; //generates a number between 1 and 10
cout<<number;
system("PAUSE");
return EXIT_SUCCESS;
}
I have a question. I want to generate a lot of random numbers. Random numbers between 1-20 in groups of six where none of the numbers are the same. How would I go about doing this? :confused:
Nope...it generates a number 0 - 9.Quote:
int number = rand()%10; //generates a number between 1 and 10
How the hell do you think it would be done? Do you actually ever think about how you might figure something out on your own, or do you just wait for every one else to do everything for you?Quote:
Originally Posted by cerin
That was a rhetorical question.
Here I got to teach yet another person how to think for themselves.
1) How would you yourself generate groups of six numbers?1a) Break that down into small steps.2) How would you turn those steps into code?
1b) Write it out.
3) How would you ask for help with that code after you've tried on your own?(Here's a hint, it's in the forum Announcements.)
Personally, I'd grab six D20s and roll them. If I didn't have six of them, which I do, I'd probably roll one D20, six times. Hm... I bet I could think of how to do the same thing in C++ six times. For instance, while I wasn't done, I'd repeat that same task over and over again. You know, like in a loop...
Quzah.
I guess for something that only uses 6 numbers you could do something sloppy like add each random number you generate to an array and for each generation check that array to see if the new number is in that array. If not, you've got a unique random number.
That is only going to be okay with a small array like in your case, with 6 numbers. If you're dealing with more numbers, I might generate a huge array of random numbers and then sort them deleting duplicates. Then shuffle.
I'm fairly n00b myself, however, and there is probably a MUCH more efficient way. But one thing I have learned is to think for myself - whether or not I'm wrong is not relevant. If I get a workable solution, that's great. Then, as I get better, I can improve it.
A fun way is to create a vector of (n) ints, from 0 to (n - 1). To get a unique random number, find a random number from 0 to (size of vector - 1), and use it to index the vector; copy the number into your array or whatever, then remove it from the vector. Repeat until you have all the unique numbers you want in that range, or until you run out of numbers.
It isn't super efficient, but the absolute worst-case scenario (n^2 / 2 copy operations) is better than the purely theoretical worst-case scenario of the other method (infinity randoms) :D