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:rocess()
{
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.
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:rocess()
{
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.