Thread: How to generate random numbers between 65 and 75

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    How to generate random numbers between 65 and 75

    Hello,
    please explain me the syntax of how to generate random numbers between 65 and 75 in C?

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    65 <= x <= 75
    Code:
    int x = (rand() &#37; 11) + 65;
    You might want to initialize the random seed to get a new sequence of pseudo-random numbers on every run.
    The most common technique is:
    Code:
    srand( time(NULL) );
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest reading Prelude's article on Using rand(). In particular, she points out weaknesses in xuftugulus' suggestions that you may want to be aware of.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Thanx for the very nice link laserlight. It really confuzzled me bedazzled head and i will use my own RNG from now on.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    I suggest reading Prelude's article on Using rand(). In particular, she points out weaknesses in xuftugulus' suggestions that you may want to be aware of.
    Although I'm sure the article is correct, I still have no idea why using (rand() % n) is wrong or what that whole thing about the low order bits was talking about? I also don't know why seeding it with time() wouldn't work very well (assuming you wait > 1 second between seeds)?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I still have no idea why using (rand() &#37; n) is wrong or what that whole thing about the low order bits was talking about?
    Suppose you want the range to be [0,3). Using the rand() % n method, n = 3.

    Suppose also that RAND_MAX is 3. Then we have the following possibilities:
    0 % 3 = 0
    1 % 3 = 1
    2 % 3 = 2
    3 % 3 = 0

    This means that half of the time you will get 0 as the random number generated. If you were looking for a uniform distribution, this obviously is not ideal.

    Of course RAND_MAX is much larger than this, so the effects of the non-uniform distribution are less pronounced.

    I also don't know why seeding it with time() wouldn't work very well (assuming you wait > 1 second between seeds)?
    Prelude's suggestion does seed with the return value of time(), only that it seeds with a hash of the bytes of the value returned by time().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    Prelude's suggestion does seed with the return value of time(), only that it seeds with a hash of the bytes of the value returned by time().
    That's the part I don't get. How would that be any better than using time() by itself?
    The article says the first random number could be the same for up to about 13 minutes by just using time(). Why?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How would that be any better than using time() by itself?
    To have guaranteed portability.

    The article says the first random number could be the same for up to about 13 minutes by just using time(). Why?
    Probably implementation dependent on how srand() works.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by cpjust View Post
    That's the part I don't get. How would that be any better than using time() by itself?
    The article says the first random number could be the same for up to about 13 minutes by just using time(). Why?
    I read the article to much time ago - so I do not remember the reason that Prelude was giving. But If we look at srand - it takes unsigned long
    So when time() returns 8 bytes value - you will take only half of it... And this half could stay the same for very long time...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's the part I don't get. How would that be any better than using time() by itself?
    If you're only getting one random number -- it probably wouldn't make much of a difference. But if you're generating more than one number, starting a random number generator is better than just querying the time all the time. (Sorry.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by dwks View Post
    If you're only getting one random number -- it probably wouldn't make much of a difference. But if you're generating more than one number, starting a random number generator is better than just querying the time all the time. (Sorry.)
    Actually, if you're only getting one number and that number doesn't change for, say, 17 minutes, that matters quite a lot. You can go through several die rolls with that kind of time.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by dwks View Post
    If you're only getting one random number -- it probably wouldn't make much of a difference. But if you're generating more than one number, starting a random number generator is better than just querying the time all the time. (Sorry.)
    I wasn't talking about using time() as a random number. I was talking about using time() as the seed for srand().

    Quote Originally Posted by vart View Post
    I read the article to much time ago - so I do not remember the reason that Prelude was giving. But If we look at srand - it takes unsigned long
    So when time() returns 8 bytes value - you will take only half of it... And this half could stay the same for very long time...
    An 8 byte value? time() returns a time_t, which is a long (which is usually 4 bytes).
    But either way, even if it took half, it would only take the lower half right?

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> An 8 byte value? time() returns a time_t, which is a long (which is usually 4 bytes).

    time_t is only guaranteed to start at the epoch of computing (January 1, 1970) and be an arithmetic type. time_t as double would be an awkward, but Standards conforming implementation that isn't exactly mungeable into a unsigned int type. I haven't seen anything too different though and it is probably somewhat an irrational concern. OTOH, it is nice to have a seed which is not so bound to the clock: for one thing, hashing it makes it more difficult to predict rand()'s sequence as long as the hash algorithm is kept somewhat of a secret or is cryptographically strong anyway.

    Once you figure out how a prng works and how it's seeded, predicting things is easy because you can imitate its' behavior, look for patterns, and cheat.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It really confuzzled me bedazzled head and i will use my own RNG from now on.
    Only if you need numbers that are less pseudo-random and more random than rand() can provide.
    There is really nothing wrong with rand(), despite what you may be told. The problem is with the people who use it, and how they use it incorrectly and without having all of the facts.
    I wasn't talking about using time() as a random number. I was talking about using time() as the seed for srand().
    Yes, I'm sorry, I misunderstood your post. And it was rather flawed anyway, as citizen pointed out.

    So . . . why is this
    Code:
    unsigned time_seed()
    {
      time_t now = time ( 0 );
      unsigned char *p = (unsigned char *)&now;
      unsigned seed = 0;
      size_t i;
    
      for ( i = 0; i < sizeof now; i++ )
        seed = seed * ( UCHAR_MAX + 2U ) + p[i];
    
      return seed;
    }
    
    srand ( time_seed() );
    better than this?
    Code:
    srand((unsigned)time(0));
    That was your question, right?

    The general idea of a hash function is that any input will generate a unique output. Ever seen MD5 sums? Change one byte, and the entire thing changes. This particular hash function just happens to be very simplistic; but using this sort of thing for a random number generator seed would probably be a very good idea.

    http://en.wikipedia.org/wiki/Cryptog..._hash_function
    http://en.wikipedia.org/wiki/Hash_function
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Maybe I'd understand this better if I knew how rand() & srand() actually work.
    This all seems like it should be the job that srand() should be doing, so that passing it 123456 should generate a completely different set of random numbers than passing it 123457...

    BTW, what does the 2U in that code mean? I've never seen anything like that in any code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File IO
    By Jack1982 in forum C++ Programming
    Replies: 9
    Last Post: 10-15-2007, 01:14 AM
  2. binary data file
    By tucker81 in forum C Programming
    Replies: 8
    Last Post: 06-08-2006, 12:50 AM