Thread: Rand() questions

  1. #1
    Registered User mill1000's Avatar
    Join Date
    Nov 2001
    Posts
    43

    Rand() questions

    does rand()%11 give me a number between 1-10?

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    No, it gives you a number 0-10.
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    And it reduces the randomness of the numbers.

  4. #4
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    i dont know exactly what to do, but having srand( GetTickCount() ); somewhere before it makes it random based on the time, so the chance of getting the same random number is much lower. but i dont know exactly how to do it
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    Code:
    #include <time.h>
    
    .....
    
    srand(time(0));

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It's a secret.
    Don't tell anyone
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    11
    try this...
    Code:
    #include <time.h>
    #include <stdlib.h>
    
    int main (void)
    {
     int Numb;
    
     srand (time (NULL));
     Numb = (rand () % 10) + 1;
    
     while (Numb > 10)
     {
       Numb--;
     }
    
     return 0;
    }
    “The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs.” - Joseph Weizenbaum

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
      Posted by dsig111
      Numb = (rand () % 10) + 1;
    
     while (Numb > 10)
     {
       Numb--;
     }
    What is that supposed to achieve?

    >>rand() % 10
    will give a number between 0 and 9. You then add one to it, so effectively, it's giving a number between 1 and 10.

    So what's the "while" loop for?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    11
    I forgot got thrown off by the %11
    “The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs.” - Joseph Weizenbaum

  10. #10
    Registered User mill1000's Avatar
    Join Date
    Nov 2001
    Posts
    43
    thx but this code produced 9 in the begining and when another function calls the function it is in it rises to 10. any sugjestions on how to stop it from aways being nine and on how to reset it when the function ends
    Code:
    int rnum=rand()%11+1;

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First off, did you read the two links I gave you?

    Now, from your comments:
    >>sugjestions on how to stop it from aways being nine<<
    If it's random, it'll do what it wants to. You might get any number between min and max.

    >>how to reset it when the function ends<<
    You can't "reset" it as such, only reset it back to the start of the psuedo random number list, but that isn't recommend.

    If the links don't help you, explain what you're trying to do, and post some of your code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    11
    the srand function sets a seed to randomize off of and when using time to do it it will set the seed different ly each time and stop using % 11 it returns 0-10 snd when add 1 it returns 1-11...

    Code:
    srand (time (NULL)); //must have stdlib.h and time.h
    
    int rnum = (rand () % 10) + 1;
    the above will produce trully random values of 1 -10.
    “The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs.” - Joseph Weizenbaum

  13. #13
    Registered User
    Join Date
    May 2003
    Posts
    11
    also if you want to prevent it from repeating the last value use a while statement...

    Code:
    #include <time.h>
    #include <stdlib.h>
    
    int main (void)
    {
     int rnum, p_rnum;
    
     srand (time (NULL));
     rnum = (rand () % 10) + 1;
     
     /* later on */
    
     do
     {
       srand (time (NULL));
    
       p_rnum = rnum;
       rnum = (rand () % 10) + 1);
     } while (p_rnum == rnum);
    
     return 0;
    }
    “The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs.” - Joseph Weizenbaum

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>srand (time (NULL)); twice
    You shouldn't be calling srand() twice in the same program. This is also in the FAQ.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User mill1000's Avatar
    Join Date
    Nov 2001
    Posts
    43

    Question

    Im so confused yes i did go to the links but can some one simplify the srand and time i dont get it

Popular pages Recent additions subscribe to a feed