Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Random numbers Posted by Genlisea on 12 Dec 2007 at 9:33 AM
How can you generate a random number in C++?
Report
Re: Random numbers Posted by velius on 12 Dec 2007 at 10:10 AM
: How can you generate a random number in C++?
:
using rand() and its variants in the C++ header cstdlib or the C header stdlib.h


We'll be an army of theives
Of self-freed slaves
Of mild-mannered maids
We'll fight with whispers and blades
So get ready, a new day is dawning
- The New Wild West -- Jewel
Report
Re: Random numbers Posted by Genlisea on 14 Dec 2007 at 6:23 AM
Thanks for answering! Is there a way to set parameters within which the random number is generated?
Report
Re: Random numbers Posted by Lundin on 14 Dec 2007 at 7:35 AM
rand() gives a float number. Use the integer modulo operator to get the interval you want:

rand() % x; /* gives a number from 0 to x-1. */

Report
Re: Random numbers Posted by MatthewD on 15 Dec 2007 at 1:54 PM
: rand() gives a float number. Use the integer modulo operator to get
: the interval you want:
:
: rand() % x; /* gives a number from 0 to x-1. */
:
:

You can see a discussion of easily generating random number in this FAQ:

http://www.givemefish.com/Downloads/Download_cppFAQ/Download_cppFAQ.php

Matt.
Report
Re: Random numbers Posted by Genlisea on 15 Dec 2007 at 6:20 PM
I got the interval to work, but every time I run a given program it uses the same random number; is there a way to make it a different number every time I run it?
Report
Re: Random numbers Posted by velius on 17 Dec 2007 at 5:28 AM
: I got the interval to work, but every time I run a given program it
: uses the same random number; is there a way to make it a different
: number every time I run it?
:
You have to first seed the random generator, usually done with the current time.

...
#include <ctime>
#include <cstdlib>
using namespace std; // include the entire namespace
...
time_t curTime;
time(&curTime);
srand(curTime);
...


That'll get you the system time and let you seed rand() via srand() before actually calling rand() itself.


We'll be an army of theives
Of self-freed slaves
Of mild-mannered maids
We'll fight with whispers and blades
So get ready, a new day is dawning
- The New Wild West -- Jewel
Report
Re: Random numbers Posted by Genlisea on 21 Dec 2007 at 8:05 AM
It worked to change the number each time the program is used, but I don't quite understand why. I was wondering if you could explain it to me?
Report
Re: Random numbers Posted by velius on 21 Dec 2007 at 8:10 AM
: It worked to change the number each time the program is used, but I
: don't quite understand why. I was wondering if you could explain it
: to me?
:

That works via the "current time" and therefore every time the time changes it sets the seeded value (where to begin) to be a different location with in the psuedo-random list. Thus when rand() is called it calls from somewhere else in the list of random values rather than starting at the beginning of the list each time.


We'll be an army of theives
Of self-freed slaves
Of mild-mannered maids
We'll fight with whispers and blades
So get ready, a new day is dawning
- The New Wild West -- Jewel
Report
Re: Random numbers Posted by Lundin on 21 Dec 2007 at 8:16 AM
The philosophical explanation is that there is no such thing as random numbers. Everything in reality is deterministic.

For the programmer, this means that he has to fake it and make deterministic numbers appearing as random as possible. To do this, you can take the time and date from the PC, put those number values in some nonsense-algorithm which does a lot of calculations on them, until the time and date is beyond recognition. This becomes the so-called random number.

So when you are calling srand(), all you do is to give the C library functions the time/date needed for the above.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.