|
-
Oct 24th, 2002, 12:10 AM
#1
Thread Starter
Addicted Member
random number generation
how to generate random number between 0 to 100 in vc++?
-
Oct 24th, 2002, 06:02 AM
#2
Frenzied Member
#include <stdlib.h> //for rand func
#include <time.h> //time func used in seeding
int main()
{
srand(time(NULL)); //seeds the number
int mynum = rand()%100+1; //gets the random num
cout<<mynum<<endl; //outputs it
return 0;
}
-
Oct 24th, 2002, 07:59 AM
#3
Monday Morning Lunatic
If you're going to post C++, you need to have:
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main() {
srand(time(NULL)); //seeds the number
int mynum = rand() % 100 + 1; //gets the random num
cout << mynum << endl; //outputs it
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 24th, 2002, 08:49 AM
#4
And this creates a random number from 1 to 100 (both inclusive), if you want to have from 0 to 100 (both inclusive) you need
rand() % 101
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.
-
Oct 24th, 2002, 11:26 PM
#5
Thread Starter
Addicted Member
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
|