Random number generator

Mar 9, 2009 at 7:09pm
hello, is there a such command that will take for example : (int n) and apply a random number, maybe 1-1000 into n's place? Thanks
Mar 9, 2009 at 7:15pm
C has a mediocre RNG called rand() you can use. It'll be fine for something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>  // for rand/srand
#include <ctime>   // for time

int main()
{
  srand((unsigned)time(0));   // seed RNG at startup -- if you don't do this
                              // the "random" numbers will be predictable.
                              //   Using time to seed is typical

  // to get a random number between [0-999]
  int num = rand() % 1000;

  // or if you want [1-1000] instead:
  num = (rand() % 1000) + 1;

  return 0;
}
Mar 9, 2009 at 7:20pm
Thanks, this is what i was looking for, i will test it out now
Topic archived. No new replies allowed.