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?
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?
Code:#if _emo #define stereo_type_i_dislike #endif
any ?uestions?Code:int x = rand () % 10; // max is 9
hasafraggin shizigishin oppashigger...
how do you make a maximum of....well......10?
Code:#if _emo #define stereo_type_i_dislike #endif
change the '10' to '11'...
hasafraggin shizigishin oppashigger...
> doubleanti
> title: grand wierdo
I believe it's spelled weirdo
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
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.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
heh... it's all part of the wierdosity man... all part of it...
hasafraggin shizigishin oppashigger...
Simple bluehead...go to my message board I'll tell you how the modulus operator works with rand()
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.
Last edited by Leeman_s; 11-10-2001 at 12:40 AM.
Wrong. a % b never equals b: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; }
- lmov
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...
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) over this. Make a program that generates some random numbrs and see where the boundries are.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.