Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2004
    Location
    kuala lumpur
    Posts
    18

    how to to generate random number within 0 to 10 in c language

    how to to generate random number within 0 to 10 in c language
    can i use rand() to make it? how to use?
    can i set the range for rand()?
    if i just use rand() it make generate 2000++ possibilityvalue
    Please help me.
    thank you

  2. #2
    Join Date
    Jan 2006
    Posts
    2
    I copied this example from the help pages, was quicker than writting my own...i've editted it for 10 (was 100 in the example)

    Obviously replace printf with which ever use you need the numbers for


    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>

    int main(void)
    {
    int i;

    randomize();
    printf("One random number from 0 to 9\n\n");
    printf("%d\n", rand() % 10);
    return 0;
    }


    =======
    for 2000+ use


    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>

    int main(void)
    {
    int i;

    randomize();
    printf("2000 random numbers from 0 to 9\n\n");
    for(i=0; i<2000; i++)
    printf("%d\n", rand() % 10);
    return 0;
    }



    =======
    for numbers between 10 -> 20 use ===> (rand() % 10) + 10;
    Last edited by AlkieDropout; 01-13-06 at 04:16. Reason: typo correction

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •