• (2 Pages)
  • +
  • 1
  • 2

Producing Random Numbers Rate Topic: **--- 4 Votes

#1 Jessehk  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • 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? 9
  • +

Replies To: Producing Random Numbers

#2 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

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

Reputation: 180
  • View blog
  • Posts: 4,667
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 IdealistTCO  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 zoreau  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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.
Was This Post Helpful? 2
  • +
  • -

#6 DoEds  Icon User is offline

  • New D.I.C Head

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

Posted 30 June 2009 - 07:38 AM

This is helpful...I can make a small game with this...
Was This Post Helpful? 0
  • +
  • -

#7 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 305
  • View blog
  • Posts: 1,507
  • Joined: 16-May 09

Posted 27 July 2009 - 04:41 AM

Thanks, it was very useful.
Was This Post Helpful? 0
  • +
  • -

#8 0pt1cKill3r  Icon User is offline

  • New D.I.C Head

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

Posted 18 October 2009 - 08:00 AM

i think you have to add :

system("pause");


before : return 0;
Was This Post Helpful? 0
  • +
  • -

#9 nanowire  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 Astraeus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#11 dr.tiberius  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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??
Was This Post Helpful? 0
  • +
  • -

#12 Astraeus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#13 meghrajsinghbeniwal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

Was This Post Helpful? 0
  • +
  • -

#14 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

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

Posted 08 March 2010 - 09:59 PM

1. We don't email answers.
2. Learn to read:

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.

Was This Post Helpful? 0
  • +
  • -

#15 Guest_albert76*


Reputation:

Posted 02 July 2010 - 12:21 PM

Generate a random number within range? [C/C++ snippet]
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2