How to Add a random feature

Posts 1–9 of 9 · Page 1 of 1
How to Add a random feature
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?
Nice explanation.

/sarcasm end.

Please increment your IQ to above 25. If you call that an explanation, then what I'm writing right now is what I call a book.
Quote Originally Posted by crushed View Post
Nice explanation.

/sarcasm end.

Please increment your IQ to above 25. If you call that an explanation, then what I'm writing right now is what I call a book.
Give him a break, he's probably proud of what he's made. Don't ruin it for him.
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. >_>
Generate a random feature? Since when is this considered a feature?

Quote Originally Posted by crushed View Post
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
Quote Originally Posted by Hell_Demon View Post
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';
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
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.
Quote Originally Posted by crushed View Post
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.
they both do the same thing, show you the remainder, just as the operator % does.

100%40=20
100-40-40=20
(100/40-floor(100-40))*40 = 20
temp1 = float, so 100/40 = 2.5
temp2 = integer, so 100/40 = 2
temp1 = temp1-temp2(so 2.5-2, which is 0.5)
temp1*40=20(the remainder)
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.
Posts 1–9 of 9 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us