Generating Random Numbers

This is a discussion on Generating Random Numbers within the C++ Programming forums, part of the General Programming Boards category; Is there any simple way to generate random numbers in C++? I see all these generators like Mersenne(sp) Twister, but ...

  1. #1
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50

    Generating Random Numbers

    Is there any simple way to generate random numbers in C++?

    I see all these generators like Mersenne(sp) Twister, but all I need is something to let me generate a number between 1 and 20.

    Thanks.

  2. #2
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Code:
    #include<iostream>
    #include<ctime>
    
    int main()
    { 
             srand(time(0));
             std::cout<<(rand()%20)+1;
             std::cin.get();
             return 0;
    }

  3. #3
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    Excellent, thats exactly what I needed.

    One more question, is there a way to bind that number to a variable?

  4. #4
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Sure -

    Code:
    #include<iostream>
    #include<ctime>
    
    int main()
    { 
             srand(time(0));
             int x = rand()%20+1;
             std::cout<<x;
             std::cin.get();
             return 0;
    }

  5. #5
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    Thanks, thats perfect!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Beware rand(). It's deceptively simple, and tricky to get right. In fact, once you use random numbers for more than a quick selection of a few numbers, you'll begin to see why everyone prefers to find a better generator.
    My best code is written with the delete key.

  7. #7
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    and what exactly would be a 'better generator'?
    (i've been looking :-[ lol)


    //edit - like the link your provided? lol
    btw, the code you have at the bottom of your page works wonderfully
    Last edited by willc0de4food; 08-19-2005 at 10:19 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  8. #8
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    The 'easy' code probably works fine in most cases but if I'm trying to pick a random student from a pool of 80, how 'non-random' is it?

    I'm going to try to test it, look for a new thread.

    -JM

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >and what exactly would be a 'better generator'?
    This is arguably the best generator for random numbers presently, but this is surprisingly good as well, despite the bad reputation that simpler linear congruential generators (like rand() is usually implemented as) have.

    >The 'easy' code probably works fine in most cases
    It depends heavily on the quality of rand(). If it's decent then the easy code probably will work just fine for simpler cases.

    >how 'non-random' is it?
    Once again, it depends on the quality of the generator and how you use it. If you need guaranteed good randomness then rand() is a tossup because it isn't required to be very good.
    My best code is written with the delete key.

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >and what exactly would be a 'better generator'?
    And, you can't forget LavaRnd
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Unlike Blum Blum Shub, the algorithm in its native form is not suitable for cryptography. For many other applications, however, it is fast becoming the random number generator of choice.
    - http://en.wikipedia.org/wiki/Mersenne_twister


    anyone have sample code for blum blum shub?

    hahaha...thats really funny to say.
    Registered Linux User #380033. Be counted: http://counter.li.org

  12. #12
    Dragoon Lover wyvern's Avatar
    Join Date
    Jul 2005
    Location
    dragooncity
    Posts
    28
    another way:

    Code:
    int x = rand% 21;   // instead of 20+1 put 21 dont be "#$ lol

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    That does not work (aside from the missing paren). The range of your function is [0, 20], while the desired range for the function is [1, 20].

    And, it uses the "less random" low order bits.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  14. #14
    Dragoon Lover wyvern's Avatar
    Join Date
    Jul 2005
    Location
    dragooncity
    Posts
    28

    dah

    then make 21 +1 not (20+1)+1

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Then that generates values in the range [1, 21]. rand() % 20 generates values in the range [0, 19]; noting that % has higher precedence than +, we see that (rand() % 20) + 1 == rand() % 20 + 1 has values in [1, 20].

    But nevertheless, you should see Prelude's tutorial (linked above) for the way you should use rand, to avoid using only the less random low-order bits.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

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. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Criteria based random numbers
    By alkisclio in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2006, 12:29 PM
  4. Random Number Generating
    By K.n.i.g.h.t in forum C Programming
    Replies: 9
    Last Post: 01-30-2005, 01:16 PM
  5. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM

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