PDA

View Full Version : RandomNumberGenerator



Forever
11-01-2001, 03:17 PM
Can someone tell me what is worng with this code?it comes upwith a number but dosent come up with different numbers
Im using Dev C++4.0 in windows Thanks

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<conio.c>
void main(){
int loop,myRandom;
//randomize
myRandom=rand()%80;
cout<<"Random Number"<<myRandom<<endl;
for(loop=0;loop<100;loop++){
gotoxy(myRandom,rand()%22);
cout<<"";
}
char myQuit;
cin>>myQuit;
}

The V.
11-01-2001, 03:22 PM
You need to seed the generator. See the FAQ for how to use the time to seed the generator.

Forever
11-01-2001, 05:06 PM
Thanx

Forever
11-05-2001, 11:24 AM
i changed the code and it works only 1 problem it dosent stay up for more than a second before it closes! can someone tell me whats wrong#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<conio.c>
#include <stdio.h>
void main(){
{
srand(time(NULL));
printf("A random number from 0 to 99: %d", rand() % 100);
return 0;
}
int loop,myRandom;
//randomize
myRandom=rand()%80;
cout<<"Random Number"<<myRandom<<endl;
for(loop=0;loop<100;loop++){
gotoxy(myRandom,rand()%22);
cout<<"";
}
char myQuit;
cin>>myQuit;
}

Hillbillie
11-05-2001, 12:37 PM
Your program executes the srand(); and printf(); statements then hits that blasted return 0;. I suspect that's your problem. Return 0;'s belong at the end of the program :D

Hillbillie
11-05-2001, 12:52 PM
Also, it wouldn't hurt to make the main function of type, int.

Forever
11-05-2001, 01:35 PM
THX ill try that =)