Thread: Random integral numbers from uniform distribution

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    1

    Question Random integral numbers from uniform distribution

    I have to write a c++ program and have a problem only with the following part

    for that purpose, you will need two random integral numbers from uniform distribution on the set: 1,2,3,4,5,6,7,8,9 (those two numbers have to be generated with equal probability)
    I don't know math as good as c++, also I'm not allowed to use c++11. I've written the following simple code, but I doubt if it's correct?

    Code:
    srand(time(0));
    int first_number = rand() % 10;
    int second_number = rand() % 10;

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Edit:
    i'm dumb. brb.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, it is not correct. The first problem is due to the range: rand() % 10 would result in integers in the range [0, 10), but you want integers in the range [1, 10). This can be easily fixed by:
    Code:
    int first_number = rand() % 9 + 1;
    But the above is still not likely to be correct in that while the two numbers would be generated with equal probability, the uniform distribution requirement might not be satisfied due to bias introduced when you used operator% in that way.

    Read Prelude's article on using rand() for a simple solution, though one that has its own drawbacks. You could think of how you could avoid this drawback while maintaining a uniform distribution.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The answer I came up with is: it's impossible. Give up. rand() in itself is so biased you're unlikely to get anything in the range [1, 10). What's worse is that rand is not deterministic. Change compilers, change operating system and everything fall into pieces again. You're not going to be able to pick two numbers that confirms to a uniform distribution unless you use a 3rd party library or C++11. Forget rand ever existed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Not impossible. Code can be written to maintain the distribution provided by rand to a smaller domain - for a sufficiently large n anyways - Random function confusion

    gg

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Color me surprised that something like that actually works... but it does. So I guess I'll have to retract that impossible statement.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    Color me surprised that something like that actually works... but it does. So I guess I'll have to retract that impossible statement.
    This has been discussed here several times before though, as well as in my link to Prelude's article. The solution (or fixed variants thereof, if you read further in the thread) offered by Accelerated C++ is hardly the only solution.
    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

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Why don't you implement a Mersenne Twister? It's really straightforward.
    Devoted my life to programming...

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    This has been discussed here several times before though, as well as in my link to Prelude's article. The solution (or fixed variants thereof, if you read further in the thread) offered by Accelerated C++ is hardly the only solution.
    The only one I remember from Prelude's article is to call rand until you get the number you want, which unsurprisingly does not always work since rand() is not a uniform distribution.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    The only one I remember from Prelude's article is to call rand until you get the number you want, which unsurprisingly does not always work since rand() is not a uniform distribution.
    The distribution of rand() is not specified, but as far as I know it is always assumed to be a uniform distribution. If it is not a uniform distribution, then none of the fixes suggested will result in a uniform distribution.

    The flaw in calling rand() until you get the number within the reduced range that you want is that with a sufficiently small range, this would be terribly inefficient, and it might even be plausible that a particular seed might result in a sequence that does not have any numbers within that range. Hence, I suggested that "you could think of how you could avoid this drawback while maintaining a uniform distribution".
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random variable with exponential distribution
    By momnia in forum C Programming
    Replies: 4
    Last Post: 01-07-2013, 09:42 AM
  2. Ranom numbers having uniform distribution
    By edesign in forum C Programming
    Replies: 9
    Last Post: 08-16-2009, 05:56 AM
  3. mathematical random distribution
    By nicodemus in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2002, 06:13 PM
  4. random number with lognormal distribution?
    By carrie in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2001, 08:31 PM
  5. Replies: 5
    Last Post: 10-12-2001, 03:51 AM

Tags for this Thread