1. ## 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.

3. 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.

4. Without a seed the computer would issue the same set of random numbers every time the program runs.

5. 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.

7. 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.

8. 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

9. 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. 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

11. 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

12. Code:
`		if (userpick = randomnumber)`

13. 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).

14. You'll probably want to do this before calling rand().
Code:
`srand(time(NULL));`

15. 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?