|
-
Aug 6th, 2002, 06:27 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Random number generation
Hiya, Im just a novice in C++ ...
What is the most efficient and effective method of generating a random number in C++ from a certain range of integers?
Last edited by cyberwarpy; Aug 7th, 2002 at 08:12 AM.
-
Aug 6th, 2002, 09:14 AM
#2
Frenzied Member
Code:
// random numbers from 1.. 10
for(int i=0;i<10;i++) printf("%d\n",rand()%10+1);
}
-
Aug 6th, 2002, 11:32 AM
#3
Monday Morning Lunatic
Remember to call srand when your program starts, using something like srand(time(NULL)).
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
-
Aug 6th, 2002, 08:48 PM
#4
Fanatic Member
what does "%d" in printf do?
and can anyone briefly state the meaning of
printf("%d\n",rand()%10+1);
, I know it prints like
1
2
3
4
5
...

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Aug 7th, 2002, 11:24 AM
#5
Monday Morning Lunatic
%d ==> decimal (i.e. integer)
rand() % 10 + 1 ==> Get a random number between 0 and 9, then add one to put it between 1 and 10.
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
-
Aug 7th, 2002, 01:25 PM
#6
Ya ya Baby!!!Me is Back
I know your problem is resolved but I got better result using gettickcount than time.
DWORD dwStart = GetTickCount();
cout << dwStart % 10 +1<< endl;
Just an other way to do it (you need to include windows.h).
-
Aug 7th, 2002, 01:29 PM
#7
Monday Morning Lunatic
Don't use GetTickCount - it generates (semi-)predictable random numbers that aren't even random 
As a seed to any other generator it's ok though. The best way is to collect entropy from the system like mouse movements, time between keypresses, etc., stuff that will be pretty close to random. Under Linux (or recent ones, anyway) you can read from /dev/random to get a stream of random data
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
-
Aug 9th, 2002, 02:55 AM
#8
Hyperactive Member
I heard they managed to get true random from quarks, not a pseudorandom number. Of course this technology isn't availible to the 'home user'!
-
Aug 13th, 2002, 06:09 AM
#9
Not from quarks, but is is a quantum effect. By putting quantum bits (e.g. a hydrogenium atom) in an undefined state, they will fall into one of the two defined states the next time you look at them - completly random and unpredictable. It works in quantum computers.
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.
-
Aug 13th, 2002, 07:44 PM
#10
Hyperactive Member
I didn't think it was quarks but I knew it had something to do with Quantum Physics.
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
|