|
-
Nov 19th, 2002, 05:36 PM
#1
Thread Starter
Addicted Member
random numbers
How do you get a random number in c++?
-
Nov 19th, 2002, 05:40 PM
#2
Monday Morning Lunatic
Code:
#include <cstdlib>
#include <ctime>
// at start of program
std::srand(time(NULL))
// when needed
int num = std::rand();
rand() returns a number between 0 and RAND_MAX (depends on your computer).
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
-
Nov 19th, 2002, 05:44 PM
#3
Frenzied Member
Code:
int i;
srand(10); // 10 is an integer value - a seed can be anything
// or you can leave it null
for(i=0;i<10;i++) printf("%d\n",rand() ); // print some random numbers
-
Nov 19th, 2002, 06:15 PM
#4
Thread Starter
Addicted Member
thanks, but what i really need to know is how to get random numbers between two parameters, like a random number between 0 and 10
-
Nov 19th, 2002, 06:17 PM
#5
transcendental analytic
that would be
std::rand()%11
for any two numbers
a+std::rand()%(b+1)
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 20th, 2002, 05:46 AM
#6
For any two numbers it would be
a+(std::rand()%(b-a))
This means including a but excluding b.
And not all standard libraries have the identifiers of the <c*> headers in the std namespace. I think VC++6 for example doesn't.
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.
-
Nov 20th, 2002, 06:17 AM
#7
Monday Morning Lunatic
Then the compiler is wrong. It's not our job to pander to broken implementations
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
-
Nov 20th, 2002, 05:29 PM
#8
Just wanted to mention it. We know that the implementation of VC++6 is very broken.
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.
-
Nov 20th, 2002, 05:29 PM
#9
And of course you have to watch out for macros, they won't ever be in any namespace.
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.
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
|