Random Integer

This is a discussion on Random Integer within the C++ Programming forums, part of the General Programming Boards category; How can I set a variable to a random number from X to Y? X being the minimum limit of ...

  1. #1
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53

    Random Integer

    How can I set a variable to a random number from X to Y?

    X being the minimum limit of the randomization.
    Y being the maximum limit of the randomization.
    --MrDoomMaster
    The kind of DooM that makes the MooD

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Read the FAQ.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    hmmm is it bad that I don't understand the FAQ on it?

    I mean it looks way too complicated...

    why can't it be like Random(10-20) lol.


    I guess if someone got into some details, of how it worked, it would help. I also have no idea what a "seed" is.

    Thanks.
    --MrDoomMaster
    The kind of DooM that makes the MooD

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Without a seed the computer would issue the same set of random numbers every time the program runs.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A pseudo-random number generator is some sort of deterministic recurrence relation (in general). The type commonly implemented for rand( ) is called a linear congruential generator (LCG), and has the form:

    x[i+1] = a * x[i] + c (mod m)

    Now, of course you must have a starting point, an x value that is fixed. So, x[0] is the initial value, and is therefore the 'seed' value.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    That is very interesting. Where can I find more information about this? Maybe I will be able to beat the kino machines

  7. #7
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    guys I'm a newbie lol... I don't even understand pointers that well yet. Those answers are a bit too technical lol.

    So lets see, a seed is a starting point in a random range?

    I can't seem to figure out the syntax for rand()

    I've tried this:

    randomnumber = rand(10)

    in hopes that this would result in a random number 0-10. Instead this gave me the following compilation error:

    Decision1.cpp(18) : error C2661: 'rand' : no overloaded function takes 1 arguments



    secondly, what is an overloaded function?


    Sorry guys for being so slow, I know it's annoying. I really appreciate anyone that helps. Thanks again.
    --MrDoomMaster
    The kind of DooM that makes the MooD

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The syntax for rand is simply:
    int r = rand( );

    This returns a random number on whatever the range of the modulus (m in the above equation is). That is, the range is 0...m-1. You can easily use modular arithmetic to get it is a range. Here's a quick demo function:

    Code:
    int random(int low, int high)
    {
      return rand() % (high - low + 1) + low ; // *edit* Fixed hideous error
    }
    Should work, but I'm a bit tired, so no guarantees.
    The high order bits in these are considered to be more random than the low order bits, so you could exploit that by getting a random floating point number (rand( ) / RAND_MAX, I believe), and then if it falls within 0...1/n it is 0, if it is in 1/n...2/n, then it is 1, etc.

    As for some sources on this stuff, The Art of Computer Programming Vol 2 is good. Numerical Recipes in C (or in C++) also has a good section ( www.nr.com ). CounterPane Labs has some stuff on cryptographically secure PRNGs ( www.counterpane.com ). Other than that, I'd say search Google (linear congruential generators, pseudo random number generators, Mersenne twister).

    Cheers
    Last edited by Zach L.; 11-18-2003 at 09:56 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,446
    randomnumber = rand() % 10;

    You should #include <cstdlib>
    rand() returns a pseudorandom number from 0 to some maximum, inclusive.
    Modulo 10 would bring this to a 0 to 10-1=9 range, albeit with a possible small bias.

    Function overloading would be having functions of the same name, but differentiated in some way, e.g. the arguments passed.

    You get that error since the compiler tries to look for a function called rand, but with an argument. It doesnt find any though.

    I suggest you read tutorials, search the web etc a bit more.

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    You probably want to use the rand function in conjunction with % (modulus).

    I think rand() returns a number between 0 and max integer.

    So number=rand() % 11; // returns a number between 0 and 10

    say rand() returned 22 then
    22 / 11 = 2 remainder 0
    23 / 11 = 2 remainder 1
    24 / 11 = 2 remainder 2
    .
    .
    .
    32 / 11 = 2 remainder 10
    33 / 11 = 2 remainder 0 // hence the range 0-10 is established
    number =rand() % (High-Low+1)+Low;

    as in the tutorial returns a number between high and low and assigns it to number

    btw an overloaded function is a function with the same name but different signature, different type of parameter
    for instance you can hav
    int funtion(int number);
    or
    int function(double number);
    the function is overloaded to handle double or int parameters and may handle things different based on the parameters.

    the mod operator returns the remainder after devision
    Last edited by curlious; 11-18-2003 at 09:55 PM.

  11. #11
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Hmm, it doesn't seem to be working. I have no idea what the math is doing, but I pasted it in my code anyway as a function, and called on that function in my mainline.

    For those who care, here is the program:

    Code:
    // IF statements
    // blah!!!!
    
    #include <iostream>
    using namespace std;
    
    int random(int low, int high)
    {
      return rand() % (high - low) + low + 1;
    }
    
    int main()
    {
    	// Initialization of Variables
    		int userpick				= 0;
    		int randomnumber			= 0;
    
    	// Prompt User Input & Processing
    		cout << "Enter a number 1-10: ";
    		cin >> userpick;
    		cout << endl;
    
    		randomnumber = random(1,10);
    
    	// IF Statement
    		if (userpick = randomnumber)
    			cout << "Great! You found the magic number!\n\n";
    		else
    			cout << "Sorry, wrong number!\n\n";
    
    	// Conclusion Statements
    		system("pause");
    		return 0;
    }
    The program compiles with no errors or warnings. The problem is, no matter what number I enter, it always says I have found the magic number! Heh, this is so frustrating! lol
    --MrDoomMaster
    The kind of DooM that makes the MooD

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,446
    Code:
    		if (userpick = randomnumber)
    that's where your problem lies.

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Err... well, there was a small error in my random function (its fixed in the original post though). The 1 is on the wrong side of the parens. Your problem is you are using the assignment ( = ) operator, not the equality test ( == ) operator, and unless the assignment is to the value 0, it will always return true (i.e. non-zero).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  14. #14
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You'll probably want to do this before calling rand().
    Code:
    srand(time(NULL));

  15. #15
    Registered User MrDoomMaster's Avatar
    Join Date
    Oct 2003
    Posts
    53
    Originally posted by anonytmouse
    You'll probably want to do this before calling rand().
    Code:
    srand(time(NULL));

    I have no idea what this is doing. What does time have to do with getting a random integer? what is with the syntax? and why null?
    --MrDoomMaster
    The kind of DooM that makes the MooD

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation
    By Graham Aker in forum C Programming
    Replies: 100
    Last Post: 01-26-2009, 07:31 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. how to generate a random integer number between 0 and 9
    By dongkhoi in forum C Programming
    Replies: 2
    Last Post: 11-07-2006, 12:49 PM
  4. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 04:16 PM
  5. random integer addition
    By strugglen in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2003, 01:00 PM

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