-
RandomNumberGenerator
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;
}
-
You need to seed the generator. See the FAQ for how to use the time to seed the generator.
-
-
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;
}
-
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
-
And another thing...
Also, it wouldn't hurt to make the main function of type, int.
-