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?
Printable View
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?
any ?uestions?Code:int x = rand () % 10; // max is 9
how do you make a maximum of....well......10?
change the '10' to '11'...
> doubleanti
> title: grand wierdo
I believe it's spelled weirdo :)
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.
heh... it's all part of the wierdosity man... all part of it... :p
Simple bluehead...go to my message board I'll tell you how the modulus operator works with rand():D
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.
Wrong. a % b never equals b:Quote:
Example: x=rand() % 10;
That means anywhere from 0 to 10, INCLUDING 10.
Code:#include <cstdio>
int main()
{
for(int i = 0; i < 20; i++)
printf("%2d % 10 = %d\n", i, i % 10);
return 0;
}
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:
http://www.cprogramming.com/cboard/s...&threadid=3867
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.