|
-
Mar 7th, 2002, 03:56 PM
#1
srand
does anyon know how to use the srand function? i have to generate a rndom number for a math prblem btwn 1..10 but i want them to be different everytime. i understand srand does this but i cat get it to work.
heres what i have
cout<<"generating random numbers between 1 and 10.....\n";
temp1 = tempA+double(rand())*((tempB-tempA)/RAND_MAX);
temp2 = tempA+double(rand())*((tempB-tempA)/RAND_MAX);
cout<<"\nThe first number is "<<temp1;
cout<<"\nThe second number is "<<temp2<<"\n";
cout<<"\nCan you figure out "<<temp1<<oper<<temp2<<" ?\n";
cin>>UserAns;
system("CLS");
return 0;
-
Mar 7th, 2002, 04:22 PM
#2
[code]
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(void){
int i, stime;
long ltime;
ltime = time(NULL);
stime = (unsigned) ltime/2;
srand(stime);
for (i=0;i<10;i++) printf("%d ",(int))10* (float)(rand()/RAND_MAX)));
printf("\n");
return 0;
}
-
Mar 8th, 2002, 12:32 PM
#3
stime;
long ltime;
ltime = time(NULL);
stime = (unsigned) ltime/2;
srand(stime);
Why?
Why not simply
srand(time(NULL));
?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 8th, 2002, 01:09 PM
#4
long and int are not the same for the compiler that was written for. Avoids overflow.
In VC++ you would not need to do that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|