d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Srand() In C++
Add Reply New Topic New Poll
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
Dec 9 2013 04:37pm
do we have to use Srand function after each rand? I'm always getting the same number running multi thread
Member
Posts: 6,635
Joined: Jun 27 2010
Gold: 2.00
Dec 9 2013 05:43pm
try using

srand(time(0));

just need this one once in your main function
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Dec 9 2013 07:34pm
Use this. It will give you real random numbers.

V_Randomizer.h
Code
#pragma once
class V_Randomizer{
private:
int seed;
long ran1idnum;
int qd2idnum;
public:
V_Randomizer();
~V_Randomizer();
double rand(double low, double high);
int rand(int low, int high);
double ran1();
double box_muller(double m, double s);

inline double ranqd2(){
unsigned long itemp;
static unsigned long jflone = 0x3f800000;
static unsigned long jflmsk = 0x007fffff;
qd2idnum = 1664525L * qd2idnum + 1013904223L;
itemp = jflone | (jflmsk & qd2idnum);
return (double)(*(float *)&itemp)-1.0;
}

};


V_Randomizer.cpp
Code
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include "V_Randomizer.h"

V_Randomizer::V_Randomizer(){
seed = (unsigned)time(NULL);
ran1idnum = -(long)seed;
qd2idnum = -(int)seed;
srand(seed);
}

V_Randomizer::~V_Randomizer(){}

double V_Randomizer::rand(double low, double high){
return (ran1()*(high - low))+low;
}

int V_Randomizer::rand(int low, int high){
return (int) rand((double) low, (double) high);
}

#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
#define NTAB 32
#define NDIV (1+(IM-1)/NTAB)
#define EPS 1.2e-7
#define RNMX (1.0-EPS)
double V_Randomizer::ran1(){

int j;
long k;
static long iy=0;
static long iv[NTAB];
float temp;
if (ran1idnum <= 0 || !iy) {
if (-(ran1idnum) < 1) ran1idnum=1;
else ran1idnum = -(ran1idnum);
for (j=NTAB+7;j>=0;j--) {
k=(ran1idnum)/IQ;
ran1idnum=IA*(ran1idnum-k*IQ)-IR*k;
if (ran1idnum < 0) ran1idnum += IM;
if (j < NTAB) iv[j] = ran1idnum;
}
iy=iv[0];
}
k=(ran1idnum)/IQ;
ran1idnum=IA*(ran1idnum-k*IQ)-IR*k;
if (ran1idnum < 0) ran1idnum += IM;
j=iy/NDIV;
iy=iv[j];
iv[j] = ran1idnum;
temp=(float)AM*iy;
if ( temp > RNMX){
return RNMX;
}else{
return temp;
}
}
#undef IA
#undef IM
#undef AM
#undef IQ
#undef IR
#undef NTAB
#undef NDIV
#undef EPS
#undef RNMX



/* mean m, standard deviation s */
double V_Randomizer::box_muller(double m, double s) {
int idnum = -1;
double x1, x2, w, y1;
static double y2;
static int use_last = 0;

if (use_last){/* use value from previous call */
y1 = y2;
use_last = 0;
}else{
do{
x1 = 2.0 * ran1() - 1.0;
x2 = 2.0 * ran1() - 1.0;
w = x1 * x1 + x2 * x2;
}while ( w >= 1.0 );

w = sqrt((-2.0 *log(w))/ w);
y1 = x1 * w;
y2 = x2 * w;
use_last = 1;
}

return( m + y1 * s );
}

Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Dec 9 2013 07:39pm
If you're using C++ you should have a really good reason for using srand+rand pair. C++ has its own, vastly superior random number generation and the C-style rand is known to cause problems: https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

you have #include <random>( http://en.cppreference.com/w/cpp/header/random ) with its own std::random_device ( http://en.cppreference.com/w/cpp/numeric/random/random_device )
Member
Posts: 1,732
Joined: Sep 3 2007
Gold: 5,991.00
Dec 15 2013 07:25pm
Quote (eric838 @ Dec 9 2013 06:37pm)
do we have to use Srand  function after each rand? I'm always getting the same number running multi thread


Put
Code
srand(time(0));

somewhere in the beginning of the file, doing just srand(); does the same thing as srand(0); so you always get the same numbers over and over again since you use the same seed.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll