468,771 Members | 1,912 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

rand() function generates same value when instantiating objects

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.

May 18 '07 #1
7 5946
jo*************@hotmail.com wrote:
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/.../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.
May 18 '07 #2
On May 18, 2:52 pm, john_smith_1...@hotmail.com wrote:
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

May 18 '07 #3
jg
On May 18, 2:52 pm, john_smith_1...@hotmail.com wrote:
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

May 18 '07 #4
jo*************@hotmail.com wrote:
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?

--
Ian Collins.
May 18 '07 #5
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.

May 18 '07 #6
jo*************@hotmail.com wrote:
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.
May 18 '07 #7
On May 18, 11:52 pm, john_smith_1...@hotmail.com wrote:
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 19 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Brian Ross | last post: by
36 posts views Thread by Ben Justice | last post: by
36 posts views Thread by Profetas | last post: by
26 posts views Thread by Gary Wessle | last post: by
5 posts views Thread by ds | last post: by
10 posts views Thread by Fred | last post: by
10 posts views Thread by Rafael Cunha de Almeida | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.