Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 118,501 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,069 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Producing Random Numbers

 
Reply to this topicStart new topic

> Producing Random Numbers

Rating  2
Jessehk
Group Icon



post 6 Jan, 2006 - 05:13 PM
Post #1


The only way (that I am aware of) to generate random numbers in C/C++ is the following.

Random numbers in C/C++ are pretty crude. There is nothing like rand.next(1, 100).

Instead, the use of the rand() and srand() functions are invloved.

CODE


#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main() {
    srand(time(NULL));
    cout <<  rand() % 10 + 1 << endl;  //number between 1 and 10

    return 0;
}



Lets go through this. First we use the srand() function to seed the randomizer. Basically, the computer can generate random numbers based on the number that is fed to srand(). If you gave the same seed value, then the same random numbers would be generated every time.

Therefore, we have to seed the randomizer with a value that is always changing. We do this by feeding it the value of the current time with the time() function.

Now, when we call rand(), a new random number will be produced every time.

CODE


#include <ctime>
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {
    srand(time(NULL));
    
    cout << rand() << endl;

    return 0;
}



The previous code gives me numbers like 113848035.

Although this is a random number, we need to do something to reduce it to a given range.

This is accomplished by applying the modulus operator (% in most languages) to the number.

Any number "modded" by 3 will either be 0, 1, or 2.

If we want a number from 0 to 9, we would write

CODE


rand() % 10;



while adding 1 would give us 1 to 10;

CODE


rand() % 10 + 1;




The reader should now understand how to generate random numbers.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Videege
Group Icon



post 6 Jan, 2006 - 11:32 PM
Post #2
I would suggest a small paragraph explaining the actual meaning of the modulus operator as a whole rather than its specific use in obtaining random numbers within a certain range (i.e., users should learn the logic of the operator and not simply that "it makes this range").

Also, if you want to take it to the next level, you could add sections explaining the process of random number generation (i.e. what is going on when you call rand()) as well as alternative number generation algorithms (for advanced users who wish to write their own methods for whatever reason).


Good start!
Go to the top of the page
+Quote Post

born2c0de
Group Icon



post 8 Jan, 2006 - 12:27 AM
Post #3
Nice Tutorial for beginners.
As Videege said...I think you should go ahead with a more advanced tutorial on Random Numbers next.
Cheers.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/11/08 07:45AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month