The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-26-2007, 10:16 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon13 How does random number generated in C++?

Below is the code that I'm playing around, I'm just trying to figure out on how does the Random number generator works in C++ it's really weird...

PHP Code:
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::cin;

#include <string>
using std::endl;
using std::string;

#include <iomanip>
using std::setw;

#include <cstdlib>
using std::rand;

int main() {
    for (
int i 010i++) {
        
cout << setw(10) << ( rand() % ) << endl;
    }
    return 
0;

More precisely this part:

PHP Code:
rand() % 
I tried to remove the 1, however I think it gives me from 0 to 5, why do we have to do modulus 6? How does it works?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 07-26-2007, 10:38 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
I have one thing to say first of all: the rand() function is not a member of the std namespace. It is a C function, so it is not technically a member of any named namespace. I've seen some code do things such as using ::printf; to use the printf() function, but as far as I know, it isn't actually necessary.

Here is the algorithm that you are using:
min_value + rand() % (max_value - min_value + 1)

That is, min_value would be 1 and max_value would be 6. To see the math behind it:
1 + rand() % (6 - 1 + 1)

The reason why you use modulo 6 is because rand() returns a value between 0 and the constant RAND_MAX (RAND_MAX is a constant defined in stdlib.h (cstdlib in C++), and the minimum value for RAND_MAX is supposed to be 32767). Obviously, you don't want to test the entire range of possible random numbers, just a limited range. That is why modulus is there.

In case you don't know, modulo 6 refers to the remainder in C++. That means that if you have 6 / 6 = 1, there is no remainder. However, if you do 5 / 6 = 0, the remainder is 5. Likewise, 1234 / 6 = 205 with a remainder of 4. In other words, 5 % 6 = 5 and 1234 % 6 = 4. Modulo operations return a value in the range of 0 to (second_operand - 1), where 6 would be the second_operand in the above example. That means that it would render a value in the range of 0 to 5 inclusive. Adding 1 to the result of that makes the range 1 to 6. I'm assuming that it is a dice rolling program since traditional dice have 6 sides.

Also, if that generates different values for you, I'm shocked since the random number generator hasn't even been seeded using the srand() function, which is also found in the same header as rand() and RAND_MAX.

Edit: As for how it works, I have no clue about the internals, especially since the implementation is compiler-specific. The general idea is that a value is selected from an internal table of numbers using a complex algorithm that operates on the seed value.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by rpgfan3233 : 07-26-2007 at 10:41 PM. Reason: Had to answer the last question about how rand() works.
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-26-2007)
  #3 (permalink)  
Old 07-26-2007, 10:55 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote below is directly from Deitel's book:

Quote:
For GNU C++, the value of RAND_MAX is 214748647; for Visual Studio, the value of RAND_MAX is 32767
Thanx a lot rpgfan3233 for the detail explanation, the equation helps a lot

Edit:

Quote:
min_value + rand() % (max_value - min_value + 1)
This equation right here, why there is "- min_value + 1" ??? I think it's kind of weird, so for example if I use min value of 2 then my maximum would be 6-1 = 5 !!!??? XD that will require me to do:

Code:
2 + rand() % 6 + 1
to keep the same maximum of "6" ??? I think that's kind of weird..

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by HelloWorld : 07-26-2007 at 11:00 PM.
Reply With Quote
  #4 (permalink)  
Old 07-27-2007, 12:13 AM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
That's why I just memorize the expression and use that.

In old-fashioned QuickBASIC (MS-DOS days), the keyword RND returned a float (a SINGLE, as in single-precision floating-point type) between 0 and 1. To change the values, you actually had to multiply rather than use the modulus operator:
Code:
CLS 'clear the screen
DIM Rand AS INTEGER
Rand = 1 + FIX(RND * 6)
PRINT Random 'print the value to the screen
END 'end the program
Note that the same idea that is used in C (MAXVALUE - MINVALUE + 1) works in QuickBASIC, right down to the addition of the MINVALUE to balance things out. Of course, FIX () in QuickBASIC is needed because otherwise Rand would hold a float/SINGLE when we would want an INTEGER.

As for why the "- min_value + 1" thing is there, consider this:
You want to generate some random data between 50 and 100 to test a grade program. Look at what could happen if we used just 50 for min_value and 100 for max_value, with no "- min_value + 1" added on:
50 + (rand() % 100)
50 + (98) //98 is less than 100...
148

But your range is supposed to be 50 to 100!! Now if you try with the "- min_value + 1" added back in:
50 + (rand() % (100 - 50 + 1))
50 + (rand() % 51)
50 + (49) //49 is less than 51...
99

See? If rand() generates a multiple of 51, including 0, it will result in the lowest value (50).


Edit: The part about GNU C++ and Visual Studio, it is actually more platform specific in that instance. GNU C++ on Windows uses Windows libraries at this time. This means that RAND_MAX on Windows is 32767 (0x7FFF). RAND_MAX on Linux is 2147483647 (0x7FFFFFFF).

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by rpgfan3233 : 07-27-2007 at 08:06 AM. Reason: Added in a comment and fixed my FIX () function (X and ( together create a smiley. :P)
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-27-2007)
  #5 (permalink)  
Old 07-27-2007, 08:24 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
In old-fashioned QuickBASIC (MS-DOS days), the keyword RND returned a float (a SINGLE, as in single-precision floating-point type) between 0 and 1. To change the values, you actually had to multiply rather than use the modulus operator:
LOL! We use the same thing in Java to generates double random value!

Here's how Java generates double random value:
Code:
Random r = new Random();
r.nextDouble(); // Generates double between 0.0 and 1.0
r.nextDouble() * 10; // then generates random number between 0.0 and 10.0

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by HelloWorld : 07-27-2007 at 08:26 AM.
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 05:20 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50