Thread: Rand() function

  1. #1
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104

    Rand() function

    Hi.
    Im looking for a simpley way to pick randomly one of thease numbers:
    1-2-3-4.

    I have searched on google, but finds nothing but how to get a randomnumber between 0 and some stupid max-number!

  2. #2
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Code:
    int somevariable = (rand()%4 + 1)
    rand()%X (where X is a number) gives you a number between 0 and X-1. That's why we add the +1, so you get between 1 and X.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could have always just read the FAQ entry on the subject.

    [edit]
    On an aside, did you never think to just simply add one to the number, since you know it starts at zero, and you want it to start at one? I hope you don't have to ask again when you want to generate a number starting at 2. Or 3. Or again at 4...
    [/edit]

    Quzah.
    Last edited by quzah; 12-25-2004 at 04:09 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    To answer your next question.. you probably forgot to seed your random number generator.

    Code:
    srand(unsigned(time( ));
    This is to be done only once.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To answer your next next question, you'll need a set of parenthesis around that unsigned, like this for it to actually work:
    Code:
    srand( (unsigned) time( ) );
    Oh, and of course you'll actually have to provide a value to the time function. So you really end up with something like this:
    Code:
    srand( (unsigned) time( 0 ) );
    Or, you could have just read the FAQ as I suggested, which covers srand.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Oh course since this is c++ you could be using the proper type of casting:
    Code:
    srand( static_cast<unsigned>( time(0) ) );

  7. #7
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by Da-Nuka
    Hi.
    Im looking for a simpley way to pick randomly one of thease numbers:
    1-2-3-4.

    I have searched on google, but finds nothing but how to get a randomnumber between 0 and some stupid max-number!
    Then obviously you don't know what a max number is *cough* 4 is your max number for the sequent 1-2-3-4 */cough*. It's not brain surgery...and as Quzah said, the FAQ is god...

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    And you can't forget that time_t isn't guaranteed to be convertible to unsigned int:
    Code:
    unsigned int random_seed()
    {
      unsigned int seed(0);
      time_t now = time(0);
      unsigned char *p = reinterpret_cast<unsigned char *>(&now);
    
      for (size_t i = 0; i < sizeof now; i++)
        seed = seed * (numeric_limits<unsigned char>::max() + 2U) + p[i];
    
      return seed;
    }
    
    ...
    
    srand(random_seed());
    My best code is written with the delete key.

  9. #9
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    Im not that noob
    Just lazy!

    All I needed was the function name... (first post)

    And I feel I gotta defend my self a little:
    I wasnt talking about 4 as the max number, if I had found ways to do that, the problem had been solved.

    The maxnumber I allways bumbed into was RAND_MAX.

    A example:
    http://www.phim.unibe.ch/comp_doc/c_...IONS/rand.html
    Last edited by Da-Nuka; 12-26-2004 at 03:25 AM.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Just lazy!
    That's a bad kind of lazy.

    >I wasnt talking about 4 as the max number, if I had found ways to do that, the problem had been solved.
    I don't understand. How is "get a randomnumber between 0 and some stupid max-number!" different from what you want? Unless you want a random permutation, what you've been finding is a viable solution. Give us more detail as to what you're trying to do.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Just lazy!
    We're not here to do work for you. We're here to help you with problems. If you know how to solve the problem and are just too lazy to do it yourself, you'll quickly find the same attitude towards helping you.

Popular pages Recent additions subscribe to a feed