Random numbers

This is a discussion on Random numbers within the C++ Programming forums, part of the General Programming Boards category; I have found out a code that can give random numbers but I think this code gives a random number ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    59

    Random numbers

    I have found out a code that can give random numbers but I think this code gives a random number between 0-32767.
    Could it be possible to get a random number between two values like for example: 0-30 ?


    Code:
    #include <cstdlib> 
    #include <iostream>
    
    using namespace std;
    
    int main() 
    { 
        int random_integer = rand(); 
        cout << random_integer << endl; 
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yes
    Code:
    int random_integer = rand() &#37; 31;
    edit: Also check srand() and google/search forum for more information about random numbers

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    59
    Thanks for the guidance
    Code:
    int test = 0;
    
    test = rand() &#37; 30;

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If you do % 30 you will get from 0-29. Since if rand() is 30 you will get 0 not 30. So you need 31

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    59
    I see, I didn&#180;t think about that. Thanks for the hint.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,974
    That said, I suggest reading Prelude's articles on Using rand() and Random Numbers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    This is generalyl a better random number generator than rand(). It only generates random bytes, so just generat 4 random bytes into a DWORD and then apply the aforementioned % 31.
    Attached Files Attached Files
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abachler View Post
    This is generalyl a better random number generator than rand(). It only generates random bytes, so just generat 4 random bytes into a DWORD and then apply the aforementioned &#37; 31.
    And of course, you know that franse needs a better random number generator from what has been discussed so far?

    And "better" is of course a matter of how you defeine "better". "good" random numbers are important if you are working on something where the quality of the random numbers have a commercial consequence.

    DWORD is not a standard type in anything other than Windows.

    Doing a loop of 64K for EVERY BYTE seems excessive. Is this actually a recognized method of generating random numbers, or just something you thought up yourself? There are a huge number of GOOD random number generators that have had years of research to prove that they are good, the Mersenne-Twister being one of those: http://en.wikipedia.org/wiki/Mersenne_Twister. It uses less memory and less, and only loops around for 624 iterations every 624th random number - which is about 600 * 100 times better than your code.



    --
    Mats
    Last edited by matsp; 11-20-2008 at 04:42 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    DWORD is just a macro for unsigned int, to say 'well its only available in windows' is a bit facesious, since windows is argueably the most important operating system in existance today.

    Besides, you can reduce the 64K loop to much smaller loop and still have random numbers of high enough quality for your purpose. Personally I use a much larger loop for some projects.

    And mersenne twisters are very poor quality, they repeat in reasonable time. My code will not repeat during the lifetime of the universe, not even close.
    Last edited by abachler; 11-20-2008 at 05:48 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abachler View Post
    DWORD is just a macro for unsigned int, to say 'well its only available in windows' is a bit facesious, since windows is argueably the most important operating system in existance today.
    And that is a good reason, you say? Why not use a typedef of your own that equates to unsigned int? - that way, the code is genuinely portable.

    And mersenne twisters are very poor quality, they repeat in reasonable time. My code will not repeat during the lifetime of the universe, not even close.


    So you have evidence to say that the MT paper:
    http://www.math.sci.hiroshima-u.ac.j...earticles.html
    that describes the initial implementation is not only wrong, but considerably wrong? According to that paper it has a period (which I take to be the point where it repeats) of 2^19937 - which is certainly considerably longer than I can expect to live. So where did they go wrong, seeing as this is a peer-reviewed paper, and implemented for MANY platforms in different languages - and all of the people that have been using it over the last 10 years are obviously not aware of this deficiency. Are you planning on to release a paper with your findings, so that we can learn what you know?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,974
    Quote Originally Posted by abachler
    DWORD is just a macro for unsigned int, to say 'well its only available in windows' is a bit facesious, since windows is argueably the most important operating system in existance today.
    Nonetheless, it is not the only operating system in actual use today, and in fact is not dominant in some areas of computing.

    Quote Originally Posted by abachler
    And mersenne twisters are very poor quality, they repeat in reasonable time.
    That is weird: all the literature I have read on it disagrees with you, and in fact state that the Mersenne Twister algorithms are high quality pseudo-random number generators for non-cryptographic use.

    Quote Originally Posted by abachler
    My code will not repeat during the lifetime of the universe, not even close.
    However, that does not prove that it is a high quality pseudo-random number generator, e.g., a generator that merely increments the previously generated number will also theoretically never repeat (for eternity), but obviously it does not have very "random" characteristics. Is this algorithm in the published literature?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by laserlight View Post
    for non-cryptographic use.
    Precisely.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,974
    Quote Originally Posted by abachler
    Precisely.
    Then you are mistaken: there are valid uses for non-cryptographic quality PRNGs, e.g., for simulations.

    How does the PRNG you suggested compare to say, Blum Blum Shub?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by laserlight View Post
    Then you are mistaken: there are valid uses for non-cryptographic quality PRNGs, e.g., for simulations.

    How does the PRNG you suggested compare to say, Blum Blum Shub?
    It was developed from similar foundations, but uses different methods. It has a cryptographic strength of greater than 512K bits.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,974
    Quote Originally Posted by abachler
    It was developed from similar foundations, but uses different methods.
    I see. What publications are available that describe it and provide analyses of its quality?

    Quote Originally Posted by abachler
    It has a cryptographic strength of greater than 512K bits.
    It may be due to my lack of expertise in this area, but that does not make sense to me. What does it mean for a cryptographically secure PRNG to have "a cryptographic strength of greater than 512K bits"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 09:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 09:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21