Thread: Any way to set peramiters for rand() to return?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    38

    Arrow Any way to set peramiters for rand() to return?

    I'm trying to write a simple program that has the function rand() to assign a number between 1 and 100 to int x. Right now, the only way I can get this to happen, is to have the program to check x after it is set, and if it is above 100, "try again". Can I tell rand() to simply "not return above 100". It would be a 'much' more efficent technique

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Please read the tutorials before posting!...
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ashinms
    I'm trying to write a simple program that has the function rand() to assign a number between 1 and 100 to int x. Right now, the only way I can get this to happen, is to have the program to check x after it is set, and if it is above 100, "try again". Can I tell rand() to simply "not return above 100". It would be a 'much' more efficent technique
    The tutorial that Sipher linked to suggests a technique that can introduce some bias, but it may still be acceptable for you. I suggest reading Prelude's article on using rand() for elaboration and alternatives.

    The method that I prefer is a variant of the rejection method that you have in mind (and which Prelude mentions in her article):
    Code:
    const int range_min = 1;
    const int range_max = 100;
    const int range_size = range_max - range_min + 1
    do
    {
        n = rand();
    }
    while (n >= (RAND_MAX / range_size) * range_size);
    n = n % range_size + range_min;
    If they are available to you, you can also use the pseudorandom number generator facilities from the TR1 extension to the standard library.
    Last edited by laserlight; 04-08-2011 at 11:23 PM.
    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
    Registered User
    Join Date
    Dec 2008
    Posts
    38
    Looked for it, but couldnt find the link. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An efficient approach to TicTacToe AI?
    By xofvc4rqb in forum C++ Programming
    Replies: 11
    Last Post: 01-15-2011, 05:09 PM
  2. A basic optimization request...
    By feeder74 in forum C++ Programming
    Replies: 27
    Last Post: 05-05-2010, 07:17 AM
  3. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  4. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  5. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM

Tags for this Thread