Thread: Random function confusion

  1. #16
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'll take yours as the new "best known method for an int within [0, RAND_MAX)"
    O_o

    Please don't explain either implementation in that way as the statement is entirely wrong.

    The statement, especially highlighting of the ')' character, says that you don't appreciate the domain and range of the `nrand' function.

    The `nrand' function aims to return an integer between [0, n)--including 0 and excluding `n'--where `n' is provided as an argument within a particular domain--the domain [2, RAND_MAX] for the original version. (Yes, you can pass 1 to the original `nrand', but the result is always 0 making it not a random value. The code has something of a bug; you should exclude the obvious "always the same value" from a "PRNG" by definition.) With respect to the range, the difference between the implementations is the domain: the version I posted has the domain [2, RAND_MAX - 1]. (Both implementations have the same range [0, n) for the same [2, RAND_MAX - 1] domain.) So, if you want to reference the valid range, "random int within" language, for the code I posted you would say "[0,n] as `n' within [2, RAND_MAX - 1)" or something similar.

    One way or the other, the range [0, RAND_MAX) is specifically invalid with the code I posted.

    Of course, you can increase the domain to [2, RAND_MAX) or even [2, RAND_MAX] if you desire, but the point of my implementation is the corrected bucket size which can be managed regardless of how one approaches a supported domain.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  2. #17
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Just implement a mersenne twister. Oh, look: Hodor Wears Shoes

    Problem solved =)

  3. #18
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Better yet, the "getRandomNumber()" function written by xkcd: xkcd: Random Number

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while function with scanf function confusion
    By yukapuka in forum C Programming
    Replies: 7
    Last Post: 08-08-2012, 03:49 AM
  2. confusion over use of a function
    By flyingants in forum C Programming
    Replies: 8
    Last Post: 11-12-2010, 02:26 PM
  3. Elevator Function confusion
    By applescruff in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2004, 10:14 AM
  4. Recursive function confusion
    By Sereby in forum C Programming
    Replies: 6
    Last Post: 07-22-2004, 12:45 PM
  5. Newbie function confusion...
    By funkydude9 in forum C++ Programming
    Replies: 9
    Last Post: 12-15-2002, 02:15 AM