• (2 Pages)
• 1
• 2

## Producing Random Numbers Rate Topic: 5 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=14057&amp;s=a95d8644ada828e963262f351512314a&md5check=' + ipb.vars['secure_hash'], cur_rating: 2, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Jessehk

Reputation: 11
• Posts: 24
• Joined: 06-January 06

Posted 06 January 2006 - 05:13 PM

POPULAR

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.

```#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.

```#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

```rand() % 10;

```

while adding 1 would give us 1 to 10;

```rand() % 10 + 1;

```

The reader should now understand how to generate random numbers.

Is This A Good Question/Topic? 10

## Replies To: Producing Random Numbers

### #2 Videege

• rêvant.toujours

Reputation: 6
• Posts: 1,413
• Joined: 25-March 03

Posted 06 January 2006 - 11:32 PM

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!

### #3 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 187
• Posts: 4,673
• Joined: 26-November 04

Posted 08 January 2006 - 12:27 AM

Nice Tutorial for beginners.
As Videege said...I think you should go ahead with a more advanced tutorial on Random Numbers next.
Cheers.

### #4 IdealistTCO

Reputation: 0
• Posts: 23
• Joined: 18-September 08

Posted 27 October 2008 - 02:52 PM

I got a question. Is it possible to implement the random number generation to a vector?

### #5 zoreau

Reputation: 2
• Posts: 1
• Joined: 21-April 09

Posted 21 April 2009 - 09:19 PM

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.

### #6 DoEds

Reputation: 0
• Posts: 3
• Joined: 30-June 09

Posted 30 June 2009 - 07:38 AM

This is helpful...I can make a small game with this...

### #7 Anarion

• The Persian Coder

Reputation: 387
• Posts: 1,663
• Joined: 16-May 09

Posted 27 July 2009 - 04:41 AM

Thanks, it was very useful.

### #8 0pt1cKill3r

Reputation: 0
• Posts: 3
• Joined: 18-October 09

Posted 18 October 2009 - 08:00 AM

i think you have to add :

```system("pause");
```

before : return 0;

### #9 nanowire

Reputation: 1
• Posts: 107
• Joined: 05-January 10

Posted 07 January 2010 - 12:53 PM

That was somewhat simple. Producing random numbers (in my opinion) would be under snippets.

### #10 Astraeus

Reputation: 23
• Posts: 205
• Joined: 17-November 09

Posted 07 January 2010 - 01:00 PM

Edit: Didn't realize post was that old, Sorry.

This post has been edited by Astraeus: 07 January 2010 - 01:02 PM

### #11 dr.tiberius

Reputation: 0
• Posts: 14
• Joined: 23-February 10

Posted 25 February 2010 - 05:27 PM

Nice Tutorial....... What type of program could be made using a random number generator??

### #12 Astraeus

Reputation: 23
• Posts: 205
• Joined: 17-November 09

Posted 25 February 2010 - 05:36 PM

There are a lot of possibilities. o.o

Simple ones include game-type programs like blackjack or dice, guessing games, etc.

### #13 meghrajsinghbeniwal

Reputation: 0
• Posts: 8
• Joined: 14-February 10

Posted 08 March 2010 - 04:56 PM

i have a problem with random function, each time random no is same but i want different random no. on each run because i want to use that random no. in switch case. so is it possible with any other function to generate different no. each time, means not two consecutives, please guide me, please reply on

<Removed>
it is so important because my game is under process

This post has been edited by macosxnerd101: 16 January 2011 - 09:45 AM
Reason for edit:: Email removed per site policies.

### #14 PlasticineGuy

• mov dword[esp+eax],0

Reputation: 281
• Posts: 1,436
• Joined: 03-January 10

Posted 08 March 2010 - 09:59 PM

Quote

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.

### #15 Guest_albert76*

Reputation:

Posted 02 July 2010 - 12:21 PM