Results 1 to 9 of 9
  1. #1
    HypnoticBabeTrap's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Posts
    35
    Reputation
    10
    Thanks
    28

    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
    ///////////////////////////////////////////////////////////////////////////////////

  2. #2
    Void's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Inline.
    Posts
    3,198
    Reputation
    205
    Thanks
    1,445
    My Mood
    Mellow
    Would you mind explaining to us why you used the rand() function and the modulus operator to limit possible numbers?

  3. #3
    crushed's Avatar
    Join Date
    Oct 2009
    Gender
    male
    Location
    My name is Jay. k?
    Posts
    415
    Reputation
    10
    Thanks
    113
    My Mood
    Sneaky
    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.

  4. #4
    Void's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Inline.
    Posts
    3,198
    Reputation
    205
    Thanks
    1,445
    My Mood
    Mellow
    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.

  5. #5
    crushed's Avatar
    Join Date
    Oct 2009
    Gender
    male
    Location
    My name is Jay. k?
    Posts
    415
    Reputation
    10
    Thanks
    113
    My Mood
    Sneaky
    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. >_>
    Last edited by crushed; 12-26-2009 at 01:26 AM.

  6. #6
    Void's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Inline.
    Posts
    3,198
    Reputation
    205
    Thanks
    1,445
    My Mood
    Mellow
    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.

  7. The Following User Says Thank You to Void For This Useful Post:

    crushed (12-26-2009)

  8. #7
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    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
    Ah we-a blaze the fyah, make it bun dem!

  9. The Following User Says Thank You to Hell_Demon For This Useful Post:

    crushed (12-26-2009)

  10. #8
    crushed's Avatar
    Join Date
    Oct 2009
    Gender
    male
    Location
    My name is Jay. k?
    Posts
    415
    Reputation
    10
    Thanks
    113
    My Mood
    Sneaky
    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.

  11. #9
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    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)
    Ah we-a blaze the fyah, make it bun dem!

  12. The Following User Says Thank You to Hell_Demon For This Useful Post:

    crushed (12-26-2009)

Similar Threads

  1. How to add features for warrock injector
    By hitmees in forum Visual Basic Programming
    Replies: 6
    Last Post: 08-25-2010, 09:18 PM
  2. How to add a password?
    By str1k3r21 in forum Visual Basic Programming
    Replies: 1
    Last Post: 10-20-2007, 04:16 PM
  3. how to add picture in vb6 trainer problem!!
    By 123456789987654321 in forum WarRock - International Hacks
    Replies: 1
    Last Post: 06-09-2007, 11:47 AM
  4. [TUT] How to add Oriental/Korean Fonts
    By W$t$5TA34TYTHSETH5Y5 in forum WarRock Korea Hacks
    Replies: 2
    Last Post: 05-06-2007, 11:08 AM
  5. how to add pointers
    By ragman1234 in forum WarRock - International Hacks
    Replies: 1
    Last Post: 04-15-2007, 09:51 AM