rand() function generates same value when instantiating objects

J

john_smith_1221

Hello, I need to use the rand() function to generate a random value, I
already know how to do it with srand(time(NULL)) and its "randomness"
is sufficient for me, the problem is my code requires to have that in
a struct constructor, which means that rand() will be called upon
object instantiation, my code creates objects dynamically all at once,
and that results in the SAME VALUE for that class member for each
object, that obviously is quite terrible and ruins my program.

This is some of my code to demonstrate:

struct process
{
process();
int arrivalTimeOf();
private:
int arrivalTime;
int CPUBurst;
int priority;
int waitingTime;
int turnaroundTime;
state currentState;
};

process::process()
{
srand(time(NULL));
arrivalTime=rand()%21;
CPUBurst=1+rand()%20;
priority=rand()%31;
currentState=terminated;
}
..
..
..
cin>>numberOfProcesses;
process *processesArray=new process[numberOfProcesses];

//To test for the values of arrivalTime for every object:
cout<<"\nArrival Time for P1="<<processesArray[0].arrivalTimeOf();
cout<<"\nArrival Time for P2="<<processesArray[1].arrivalTimeOf();
cout<<"\nArrival Time for P3="<<processesArray[2].arrivalTimeOf();
//And they are all the same!

I appreciate any help as I'm quite clueless what to do next, and I did
read the C FAQ and searched a bit, it lead to LCGs and whatnot,
modulus being bad (I don't care), but nothing regarding this issue. I
am using Microsoft Visual C++ 6.0, thanks.
 
G

Gianni Mariani

Hello, I need to use the rand() function to generate a random value, I
already know how to do it with srand(time(NULL)) and its "randomness"
is sufficient for me, the problem is my code requires to have that in
a struct constructor, which means that rand() will be called upon
object instantiation, my code creates objects dynamically all at once,
and that results in the SAME VALUE for that class member for each
object, that obviously is quite terrible and ruins my program.

This is some of my code to demonstrate:

struct process
{
process();
int arrivalTimeOf();
private:
int arrivalTime;
int CPUBurst;
int priority;
int waitingTime;
int turnaroundTime;
state currentState;
};

process::process()
{
srand(time(NULL));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Do you NEED to have this here ? It really only needs to be done once
each time main() is called. Why not call it in main ? If you can't
call it in main() you can initialize it in other ways. As you have
found out, putting it here is a rather pointless exercise.

Also, the books I have read suggest that rand()%N is "not particularly
random".

http://www.thinkage.ca/english/gcos/expl/c/lib/rand.html

.... but that's probably never going to really be an issue for you.

arrivalTime=rand()%21;
CPUBurst=1+rand()%20;
priority=rand()%31;
currentState=terminated;
}
.
.
.
cin>>numberOfProcesses;
process *processesArray=new process[numberOfProcesses];

//To test for the values of arrivalTime for every object:
cout<<"\nArrival Time for P1="<<processesArray[0].arrivalTimeOf();
cout<<"\nArrival Time for P2="<<processesArray[1].arrivalTimeOf();
cout<<"\nArrival Time for P3="<<processesArray[2].arrivalTimeOf();
//And they are all the same!

I appreciate any help as I'm quite clueless what to do next, and I did
read the C FAQ and searched a bit, it lead to LCGs and whatnot,
modulus being bad (I don't care), but nothing regarding this issue. I
am using Microsoft Visual C++ 6.0, thanks.
 
M

Michael

Hello, I need to use the rand() function to generate a random value, I
already know how to do it with srand(time(NULL)) and its "randomness"
is sufficient for me, the problem is my code requires to have that in
a struct constructor, which means that rand() will be called upon
object instantiation, my code creates objects dynamically all at once,
and that results in the SAME VALUE for that class member for each
object, that obviously is quite terrible and ruins my program.

This is some of my code to demonstrate:

struct process
{
process();
int arrivalTimeOf();
private:
int arrivalTime;
int CPUBurst;
int priority;
int waitingTime;
int turnaroundTime;
state currentState;

};

process::process()
{
srand(time(NULL));
arrivalTime=rand()%21;
CPUBurst=1+rand()%20;
priority=rand()%31;
currentState=terminated;}

.
.
.
cin>>numberOfProcesses;
process *processesArray=new process[numberOfProcesses];

//To test for the values of arrivalTime for every object:
cout<<"\nArrival Time for P1="<<processesArray[0].arrivalTimeOf();
cout<<"\nArrival Time for P2="<<processesArray[1].arrivalTimeOf();
cout<<"\nArrival Time for P3="<<processesArray[2].arrivalTimeOf();
//And they are all the same!

I appreciate any help as I'm quite clueless what to do next, and I did
read the C FAQ and searched a bit, it lead to LCGs and whatnot,
modulus being bad (I don't care), but nothing regarding this issue. I
am using Microsoft Visual C++ 6.0, thanks.

IIRC, you should call srand(time(NULL)) once, in your main() function,
and just call rand() everywhere else.

Michael
 
J

jg

Hello, I need to use the rand() function to generate a random value, I
already know how to do it with srand(time(NULL)) and its "randomness"
is sufficient for me, the problem is my code requires to have that in
a struct constructor, which means that rand() will be called upon
object instantiation, my code creates objects dynamically all at once,
and that results in the SAME VALUE for that class member for each
object, that obviously is quite terrible and ruins my program.

This is some of my code to demonstrate:

struct process
{
process();
int arrivalTimeOf();
private:
int arrivalTime;
int CPUBurst;
int priority;
int waitingTime;
int turnaroundTime;
state currentState;

};

process::process()
{
srand(time(NULL));
arrivalTime=rand()%21;
CPUBurst=1+rand()%20;
priority=rand()%31;
currentState=terminated;}

.
.
.
cin>>numberOfProcesses;
process *processesArray=new process[numberOfProcesses];

//To test for the values of arrivalTime for every object:
cout<<"\nArrival Time for P1="<<processesArray[0].arrivalTimeOf();
cout<<"\nArrival Time for P2="<<processesArray[1].arrivalTimeOf();
cout<<"\nArrival Time for P3="<<processesArray[2].arrivalTimeOf();
//And they are all the same!

I appreciate any help as I'm quite clueless what to do next, and I did
read the C FAQ and searched a bit, it lead to LCGs and whatnot,
modulus being bad (I don't care), but nothing regarding this issue. I
am using Microsoft Visual C++ 6.0, thanks.

You may:
1. move srand() out of the constructor and invoke it once in main; or
2. create a static member of struct process, like
class process {
static unsigned next_seed;
...
}
unsigned process::next_seed = 0;
process::process()
srand(next_seed++);
...
}

time() returns the total number of second. And within a second,
your program may be able to create all objects, which results
in the same seed value in all ctor invocation. That is why you
have the same rand value in different objects.

JG
 
I

Ian Collins

Hello, I need to use the rand() function to generate a random value, I
already know how to do it with srand(time(NULL)) and its "randomness"
is sufficient for me, the problem is my code requires to have that in
a struct constructor, which means that rand() will be called upon
object instantiation, my code creates objects dynamically all at once,
and that results in the SAME VALUE for that class member for each
object, that obviously is quite terrible and ruins my program.
Why can't you just call srand() once before you create the objects, as
you should?
 
J

john_smith_1221

Alright placing srand(time(NULL)) in main instead of in the
constructor solved the problem, I don't know how this never occured to
me, lol, maybe cuz I don't really understand this srand() weird
function, anyway thanks alot you all.
 
R

red floyd

Alright placing srand(time(NULL)) in main instead of in the
constructor solved the problem, I don't know how this never occured to
me, lol, maybe cuz I don't really understand this srand() weird
function, anyway thanks alot you all.

srand() says to initialize the pseudo-random number generator.
The problem is that because time() has a resolution of 1 second, you
were continually reinitializing to the same value over and over and over.
 
J

James Kanze

Hello, I need to use the rand() function to generate a random value, I
already know how to do it with srand(time(NULL)) and its "randomness"
is sufficient for me, the problem is my code requires to have that in
a struct constructor, which means that rand() will be called upon
object instantiation, my code creates objects dynamically all at once,
and that results in the SAME VALUE for that class member for each
object, that obviously is quite terrible and ruins my program.

Use a singleton to get the random numbers, and seed the
generator once in the constructor of the singleton.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top