Would you mind explaining to us why you used the rand() function and the modulus operator to limit possible numbers?
This is How to
Generate a random feature
// Includes //
#include <cstdlib>
#include <ctime>
///////////////////////
///Inside int main or w/e you use////
srand(time(0)); // Generate the Radom Func.
int theNumber = rand() % 100 + 1; //random number between 1 and 100
///////////////////////////////////////////////////////////////////////////////////
Would you mind explaining to us why you used the rand() function and the modulus operator to limit possible numbers?
Lol, it's not that. It's cause I was studying the same function before in some book, but then I never understood it so I gave up, but then again I never really tried hard to learn it. So I'll eventually learn it next week or something, cause I can't really program much this week. so I was hoping he'd actually explain it in detail, turns out, it's just an outline.
Kinda like buying frozen food at the supermarket, and all it tells you is to heat it up, not what temperature to heat at, or for how long. >_>
Err...I'll try and explain it.
srand(time(0)); Would be seeding the rand() function, using the time function since the time always goes up therefore the rand() function wouldn't give us the same number twice.
Now, before I explain rand(), you need to understand the modulus operator, Let's say we do 3 % 2, this will try and fit as many 2's into 3 as possible and it would return the remainder, in this case it's 1. So..if you use rand() % 100, it will give us the remainder.
So let's see, if rand() generates 10126, We get 26. If it generates 897 we get 97. Hope this clears things up.
crushed (12-26-2009)
Generate a random feature? Since when is this considered a feature?
srand sets the seed(the start position from which rand will retrieve numbers, therefor its not random, but pseudo random), time will return the current system time in miliseconds I believe, that needs to be used because rand is actually pseudo random, it picks numbers in the same order based on the seed!
If you use srand(1337), and then print out rand 10 times, restart the app, use srand(1337) again and print it out aian, it will be the same 10 numbers, because its pseudo random.
so srand(time(0)) will set the seed to the current system time, meaning it wont generate the same numbers if you restart the app.
And what do we know about the % operator?
The % operator is pretty much this:
say we have 'c = a % b';
say a was 100, b was 40 this would be what happens:Code:while((a-b)>0) { a=a-b; } c=a;
100-40>0? yes
60-30>0? yes
20-30>0? no
c = 20
another way of doing it would be:
in the case of a=100, b = 40:Code:float temp1; int temp2; temp2 = temp1 = a/b; temp1=temp1-temp2; b*temp1;
100/40 = 2.5
temp1 = 2.5
temp2 = 2(since its an integer, so it gets rounded off, to be safe use floor(temp1) as value for temp 2)
temp1=2.5-2 so temp1=0.5
40*0.5 = 20
Both methods should work as replacement for operator % ;P I know its quite useless to make it yourself, but at least it makes you know how % works
so basicly rand will be any number between 0 and RAND_MAX.
rand() % 100 will remove 100 from the number returned by rand() untill it would result in negativ number
Ah we-a blaze the fyah, make it bun dem!
crushed (12-26-2009)
Hence, this is what you call an explanation. So basically, the first program displays the output of a-b, as long as the sum is greater than 0, correct? ._.
In the second one, a is equal to b, which is equal to a/b, so they're both 2.5, just that one is an integer so it gets rounded off. Hmm, I might be a bit confused, but why is b multiplied by temp1?
That's the only part I got confused at, everything else, I understand! Thanks.
Ah we-a blaze the fyah, make it bun dem!
crushed (12-26-2009)