0

I am making a function to make different random value when I run the executable file again. This is my function to make a bit 0 and 1. The first time, I run it and get output is 0 1 1 1. And I run the .exe the output is same with prior time. What is happening? I want different output when I run again. Can you help me?

int randBit()
{
    int bit;
    double randval;
    randval = (double)rand()/(double)RAND_MAX;
    if(randval<0.5) bit=0;
    else bit=1;
    return bit;
}
Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
Jame
  • 3,746
  • 6
  • 52
  • 101

5 Answers5

4

Here is a solution using the <random> header.

std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist (0,1);
int random_number = dist(mt);

Call dist(mt) every time you need a new random number.

A.B.
  • 15,364
  • 3
  • 61
  • 64
2

You need srand function in main.

seed it with time:

  #include <time.h> //header for time

  int main(){
       srand(time(NULL)); // call only once and preferably at the start of main 

       // your code including the function randBit()

       return 0;
 }

Here is a sample code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int randBit()
{
   int bit;
   double randval;
   randval = (double)rand()/(double)RAND_MAX;
   if(randval<0.5) bit=0;
   else bit=1;
   return bit;
}

int main(){

      srand(time(NULL));
      int i=0,random=0;

      for(i=0;i<100;i++){
            random = randBit();
            printf("%d ",random);
      }

      printf("\n");  
      return 0;
}

And this is the output:

Notra:Desktop Sukhvir$ ./test

1 1 0 0 1 1 0 1 0 0 1 0 0 1 1 0 1 1 0 1 1 0 0 0 0 1 0 1 0 1 1 0 1 1 0 1 0 0 0 1 0 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 0 1 0 1 1 1 0 0 1 0 1 1 1 0 0 0 1 0 1 0 1 0 1 1 1 1 0 1 1 0 0 0 1 0 1 1 0 0 1 0 1 1 0 1

sukhvir
  • 5,265
  • 6
  • 41
  • 43
0

You need to call srand() prior to rand() first call. For Windows you can use:

srand( GetTickCount() );

And platform-independant variant:

srand( time(NULL) );
Ivan
  • 2,007
  • 11
  • 15
0

From the manpage:

The rand() function returns a pseudo-random integer in the range 0 to RAND_MAX inclusive.

This means that rand does not actually generate random values on each call, but takes a seed as an initial value and generates pseudo random values based on that seed. With a specific seed, each sequence of calls to rand() will provide the same sequence of "random" numbers.

The seed can be initialized with the srand() function, and a good approach to having a different sequence of produced numbers in each execution is to use time(NULL) as the seed.

Please note that you need to initialize the seed only once, so you may decide to do this in the main function of your program.

Cheers.

ergysdo
  • 1,139
  • 11
  • 20
0

The rand() function in C is pseudo random and thus it needs a seed. This seed can be any number but is usually the current time, sensor data or anything else that is less predictable.

The seed has a default value which result in the same sequence of "random" numbers each time you run the program. To set the seed you can use the srand() function, for example srand(time(NULL)) to use the current time as seed. The random value is caluculated from the seed and thus rand() caluculates a new one on each call based on the previous.

Jocke
  • 673
  • 3
  • 8