mardi 2 août 2016

How do you stop this random number warning?

I get warning (c++ environment)

 /Usr/main.cpp:35: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
r= rand()/static_cast<REAL>(RAND_MAX+1);
                                    ^

My code is

typedef double REAL;
REAL X=2.5;

REAL r= rand()/static_cast<REAL>(RAND_MAX+1);
xs=((REAL)(0 + static_cast<int>( r * (100*X - 0) )))/100;

I am trying to generate a random number between 0 and 2.5 within 2 decimal accuracy.

Is this warning serious? and is there a way to fix this?




Aucun commentaire:

Enregistrer un commentaire