C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,272 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,256 people online right now. 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 - 04: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 - 10: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 7 Jan, 2006 - 11:27 PM
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

IdealistTCO
*



post 27 Oct, 2008 - 01:52 PM
Post #4
I got a question. Is it possible to implement the random number generation to a vector?
Go to the top of the page
+Quote Post

zoreau
*



post 21 Apr, 2009 - 08:19 PM
Post #5
Modulus returns the remainder, ex: 5 % 2 would return 1, because 2 2's go into 5 with a difference of 1 left over.
12 % 6 = 0 6 goes into 12 twice, with no remainder
15 % 8 = 7 8 goes in once, with a remainder of 7; 15 = (8 * 1) + 7
20 % 3 = 2 3 goes into 20 6 times with a remainer of 2; 20 = (3 * 6) + 2
11 % 3 = 2

x % n will always return a value in the range of 0 to (n - 1). The returned value cannot be equal to or higher than the modulus number, because a remainder of 5 from a modulus of 5, really means an answer of 0 because the remainder can be divisible by 5 exactly once. Explaining this last part is a bit hard but it should be easy to understand anyway.
Go to the top of the page
+Quote Post

DoEds
*



post 30 Jun, 2009 - 06:38 AM
Post #6
This is helpful...I can make a small game with this...
Go to the top of the page
+Quote Post

Anarion
Group Icon



post 27 Jul, 2009 - 03:41 AM
Post #7
Thanks, it was very useful.
Go to the top of the page
+Quote Post

0pt1cKill3r
*



post 18 Oct, 2009 - 07:00 AM
Post #8
i think you have to add :

CODE
system("pause");


before : return 0;
Go to the top of the page
+Quote Post


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

 


Lo-Fi Version Time is now: 11/7/09 11:11AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month