4

I am using below code to generate random numbers in range...

int randomNumberWithinRange(int min,int max)
{
        int snowSize = 0;
        do
        {
            snowSize = rand()%max;
        }
        while( snowSize < min || snowSize > max );
        return snowSize;
    }

for(int i = 0; i < 10 ; i++)
  NSlog("@"%d",\t", randomNumberWithinRange(1,100));

If I quit my application and restart, same set of numbers are generated. How to generate different set of random numbers for every launching.

Chandan Shetty SP
  • 5,087
  • 6
  • 42
  • 63
  • 2
    think how hard it would be to debug a program if rand() returned a different sequence of values each time. – Martin York Apr 23 '10 at 14:56
  • Note that random numbers generated by rand() are not truly random. In most cases they are fine, but if you need *truly* random numbers, you need another random number generator. Many posts on SO and the google regarding this, here's one: http://stackoverflow.com/questions/1046714/what-is-a-good-random-number-generator-for-a-game – John Dibling Apr 23 '10 at 14:58
  • @martin : It is the requirement of my project. – Chandan Shetty SP Apr 23 '10 at 15:02
  • No. It is __NOT__ a requirement of your application to produce a different random sequence every time. Debugging a program with different behavior each time you run it is imposable (as you don't know what the inputs are therefore you don't know how the program reacted). Now a fully functional program it would be then be nice to able to set up to give a unique sequence. So you should do time = time(NULL); Log(time); srand(time); Then if the program crashes or does something silly. You have logged the start point and you can reproduce the problem. – Martin York Apr 23 '10 at 17:18
  • @martin : I understand what you are saying, Its game project - rocks are the obstacles to the moving car, I am using random number to place the rocks... so every time the user plays the rocks has to be in different places so it is the requirement. – Chandan Shetty SP Apr 26 '10 at 05:22

6 Answers6

11

Do something like for example srand(time(NULL)), i.e. sync it with time (in the initialisation of your program, of course).

phimuemue
  • 34,669
  • 9
  • 84
  • 115
4

You have to initialize the random number generator with a seed as pointed out.

Additionally you should avoid the loop:

int randomNumberWithinRange(int min,int max)
{
  return min + rand() % (max - min + 1);
}
Danvil
  • 22,240
  • 19
  • 65
  • 88
3

You need to seed the random number generator.

From the man page:

The srand() function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand() is called before any calls to srand() are made, the same sequence shall be generated as when srand() is first called with a seed value of 1.

RC.
  • 27,409
  • 9
  • 73
  • 93
3

You should consider using arc4random instead of rand, for many reasons, one of which is arc4random doesn't require seeding.

Jasarien
  • 58,279
  • 31
  • 157
  • 188
0

This code generates a unique random number only once.

#include <ctime>
# include <iostream>
using namespace std;


int main()
{

      int size=100;
      int random_once[100];
      srand(time(0));

      for (int i=0;i<size;i++)  // generate unique random number only once
      {
          random_once[i]=rand() % size;
          for(int j=0;j<i;j++) if (random_once[j]==random_once[i]) i--;   
      }

      for ( i=0;i<size;i++) cout<<" "<<random_once[i]<<"\t";

  return 0;
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
-1

In addition to seeding the random number generator with srand(), your general technique here is not quite right. First of all you should never use modulus for cutting the rand range. This users lower-order bits which are less random. Second, there is no need to loop. You can get the range directly. Here is how to do it:

snowSize = min + (int) (max * (rand() / (RAND_MAX + 1.0)));

frankc
  • 11,290
  • 4
  • 32
  • 49
  • Floats are not necessary here. – Danvil Apr 23 '10 at 15:07
  • No he is not. The random and srandom functions are the ones that have a more random looking low bit sequence. From the man page of random(3): "rand(3) produces a much less random sequence -- in fact the low dozen bits generated by rand go through a cyclic sequence". – JeremyP Apr 23 '10 at 15:20
  • Are you talking to me me or him? The example uses rand(3) and therefore should not use % as I said, and you are agreeing with me? – frankc Apr 23 '10 at 17:35
  • It only uses the lower-order bits if max is near a power of two. But I think your calculation is more robust. – President James K. Polk Apr 25 '10 at 01:10