Generate a random feature? Since when is this considered a feature?

Originally Posted by
crushed
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. >_>
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';
Code:
while((a-b)>0)
{
a=a-b;
}
c=a;
say a was 100, b was 40 this would be what happens:
100-40>0? yes
60-30>0? yes
20-30>0? no
c = 20
another way of doing it would be:
Code:
float temp1;
int temp2;
temp2 = temp1 = a/b;
temp1=temp1-temp2;
b*temp1;
in the case of a=100, b = 40:
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