![]() |
|
|
|
| ||||||
|
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. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| |||
| 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. Last edited by rpgfan3233 : 07-26-2007 at 10:41 PM. Reason: Had to answer the last question about how rand() works. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-26-2007) | ||
| ||||
| Quote below is directly from Deitel's book: Quote:
Edit: Quote:
Code: 2 + rand() % 6 + 1 ![]() Last edited by HelloWorld : 07-26-2007 at 11:00 PM. |
| |||
| 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 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. 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) |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-27-2007) | ||
| ||||
| Quote:
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 Last edited by HelloWorld : 07-27-2007 at 08:26 AM. |
![]() |
| Thread Tools | |
| Display Modes | |
| |