How can I get rid of the warning using rand ()? (C ++)

Whenever I use the rand function in C ++:

#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
int main(){
srand(time(0));
int n=(rand()%6)+1;
cout<<"The dice roll is "<<n<<"."<<endl;
}

      

I get a warning about converting from time_t to int on line 5:

srand(time(0));

      

Is there a way to get rid of this warning?

+1


source to share


6 answers


Actually, you have to use unsigned int with srand () :



srand((unsigned) time(0));

      

+8


source


On another note, this code:

rand()%6

      

is generally considered bad practice. the lower bits of rand () are significantly less random than the higher bits. You will get better randomness if you:

(rand() >> 8)%6

      

eg.

EDIT:

For more on this, see this note , as well as this article from Dr. Dobbs , which at least hint at the reason:

Note. Do not use

  y = rand()  %  M;

      

as this focuses on the least significant bits of Rand (). For linear congruent random number generators, which rand () often has, the lower bytes are much less random than the higher bytes. in fact, the lowest bit of the loop is between 0 and 1. So rand () can cycle between even and odd (try it). Note. Rand () do not have to be linear congruent random number generator. It is perfectly acceptable for this to be something better than this problem.



DDJ:

Most importantly, the least significant bits of the output from the normal (linear congruent) random number of generators are the smallest "Random". That is, the patterns in the least significant bits are common. Hence the conclusion from the routine roll in your discussion is not surprising. It can also be avoided by relying on the upper bit to determine the integer returned.

For example, if you want to choose a random value "true" or "false" and you used the code:

rand() % 2

      

Then you can see the results template:

1,0,1,0,1,0,1,0,1,0 (etc.)

This is obviously not random, but it is a property of the linear congruential generator that can be used. The best scheme in general (for C ++) might be to use the Boost.Random library , which supports all sorts of pluggable random generators (including the Mersenne Twister, which doesn't have this drawback).

+6


source


Use an explicit cast to get rid of the warning:

srand((int)time(0));

      

+2


source


Two side notes:

  • The standard way to include C header in C ++ is as follows: #include <cstdio>

    .
  • The parameter passed to time()

    is a pointer, and many find it NULL

    a more readable null pointer than 0

    .
+2


source


To get rid of the warning, you should use a static cast to unsigned integer.

srand(static_cast<unsigned int>(time(0)));

      

In a related note, the results of rand must be right-shifted to remove any offset in the least significant bits.

int n = ((rand() >> 8) % 6) + 1;

      

Finally, in C ++ C and standard libraries must be included as:

#include <ctime>
#include <cstdlib>

      

This will put the functions in the appropriate namespace, 'std'.

+2


source


Besides,

 rand() % 6

      

will present a slight offset. Since RAND_MAX% 6 is 1, zero and one will appear slightly more often than two to six. In this case, they are returned 5462 times for every 5461 times higher numbers are returned, so you probably won't notice. However, if the range of numbers you want is large, the offset can be significant. For example, if you did rand() % 32000

, then a number in the range 0 - 767 would double as often as 768 - 32000.

+1


source







All Articles