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

This is a discussion on Any way to set peramiters for rand() to return? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a simple program that has the function rand() to assign a number between 1 and 100 ...

  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
    1,616
    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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21