random function

• 11-07-2001
random function
How do you make a random function work?

other questions:
where do you type the variable that you assign the rndm number to?
where do you type the highest number the rndm thing will go to?
umm........
jus plain how do you do it?
• 11-07-2001
doubleanti
Code:

`int x = rand () % 10;  // max is 9`
any ?uestions?
• 11-07-2001
how do you make a maximum of....well......10?
• 11-07-2001
doubleanti
change the '10' to '11'...
• 11-08-2001
Magos
To DoubleAnti
> doubleanti
> title: grand wierdo

I believe it's spelled weirdo :)
• 11-08-2001
Stoned_Coder
Random numbers have been covered a lot in the past couple of weeks. Read some more threads. LOOK UP.Between the buttons marked faq (probably some sort of self-destruct as no-one seems to see this) and home is another that says search on it. Do you think thats there just to look pretty like the faq button?? dont u guys do any research of your own.
• 11-09-2001
doubleanti
heh... it's all part of the wierdosity man... all part of it... :p
• 11-09-2001
Xterria
Simple bluehead...go to my message board I'll tell you how the modulus operator works with rand():D
• 11-10-2001
Leeman_s
To...
To put it simple. Ok, you want a random number. Go like this:

Example: x=rand() % 10;

That means anywhere from 0 to 10, INCLUDING 10. If you want to set another boundry, go like this:

Example: x=5 + rand() % 10;

That means anywhere from 5 to 10, inclusive. Meaning 5 and 10 are included. Think of it like this 5>=rand()>=10. Those are greater-than-or-equal-to signs.

If you want it to generate random numbers in a different pattern each time, you must enter this before you do the rand() thing:

srand(time (NULL) );

That will make it a different pattern of random numbers according to the time clock. Or if you wanted you could go like this:

unsigned random;
cout<<"Enter number: ";
cin>>random;
srand(random);
x=5 + rand() % 10;

Ok that will change the patter according to what the user enters. Make sure if you do it the time clock way, include "time.h". And for srand(); you need "windows.h". Well, that just about covers it.
• 11-10-2001
lmov
Quote:

Example: x=rand() % 10;

That means anywhere from 0 to 10, INCLUDING 10.
Wrong. a % b never equals b:
Code:

```#include <cstdio> int main() {     for(int i = 0; i < 20; i++)         printf("%2d % 10 = %d\n", i, i % 10);     return 0; }```
• 11-10-2001
Magos
Yikes, lots of errors
Example: x=rand() % 10;
That means anywhere from 0 to 10, INCLUDING 10.

Nope! It is from 0 to 9 (including 9). See this link for an earlier discussion on this:

If you want to set another boundry, go like this:
Example: x=5 + rand() % 10;

That means anywhere from 5 to 10, inclusive. Meaning 5 and 10 are included.

Wrong again. That means anywhere from 5 to 14 (including 14).

Think of it like this 5>=rand()>=10. Those are greater-than-or-equal-to signs.

I want to see that number, which is greater than 10 but smaller than 5... :D

And for srand(); you need "windows.h".

Umm, srand() is in stdlib.h. Well, it IS possible that windows.h includes stdlib.h also...

Well, if you're still :confused: (confused) over this. Make a program that generates some random numbrs and see where the boundries are.