Random numbers

This is a discussion on Random numbers within the C Programming forums, part of the General Programming Boards category; I'd get really worried using any compiler which states "perhaps u should..." Quzah....

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd get really worried using any compiler which states "perhaps u should..."


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <DBOS\LIB.h>
    #include <CLIB.H>
    
    void count_nums(int *a, int range, int num);
    int my_rand(int);
    int get_max(int *a, int s);
    int get_min(int *a, int s);
    
    int main(void)
    {
        int range, num;
        /*!! foo.c:14: warning: implicit declaration of function ‘date_time_seed’ */
        date_time_seed();
        /*!! foo.c:15: warning: ISO C90 forbids mixed declarations and code */
        int a[50];
        char c;
        int s, i;
    
        printf(" * Please press r/R to run or press q/Q to exit *\n");
        /*!! foo.c:20: warning: implicit declaration of function ‘getch’ */
        c = getch();
        /*run by pressing r and exit by pressing q */
    
        if (c == 'r' || c == 'R') { /*accept both lower and upper case */
            /*move to next statment only if the input in range 1 to 20 */
            do {
                printf("Enter the range size(max=20)");
                scanf("%d", &range);
            }
            while (range < 0 || range > 20);
    
            printf("How many numbers do you wish to generate?");
            scanf("%d", &num);
            count_nums(a, range, num);
            for (i = 0; i < range; i++)
                printf("%d:%d\n", i, a[i]);
            printf("Maximum count:%d\n", get_max(a, range));
            
            printf("Minimum count:%d\n", get_min(a, range));
            printf("Expected: %d\n", (int) (num / range));
        }
        if (c == 'q' || c == 'Q')
            exit(1);
    
        return (0);
    }
    
    int my_rand(int range)
    {
        int r;
        date_time_seed();
        r = 1 + rand() % range;
        return r;
    }
    
    void count_nums(int *a, int range, int num)
    {
        int i;
        for (i = 0; i <= num; i++)
        {
            num = my_rand(range);
            *(a + num) = 0;
        }
    }
    
    /* function to find the max */
    int get_max(int *a, int s)
    {
        int i, max; 
       max= *a;
        for (i = 0; i < s; i++)
            if (*(a + i) > max)
                max = *(a + i);
        return (max);
    }
    
    /*function to find the min */
    int get_min(int *a, int s)
    {
        int i, min;
    
        min = *a;
        for (i = 0; i < s; i++)
            if (*(a + i) < min)
                min = *(a + i);
        return (min);
    }
    This is my latest program. I still couldnt run it after the second print statement.

    Please help.

    Thanks.
    NiCoLeHa

  3. #18
    Registered User
    Join Date
    Dec 2005
    Posts
    14

    Please help me!!

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <DBOS\LIB.h>
    #include <CLIB.H>
    
    void count_nums(int *a, int range, int num);
    int my_rand(int);
    int get_max(int *a, int s);
    int get_min(int *a, int s);
    
    int main(void)
    {
        int range, num;
        /*!! foo.c:14: warning: implicit declaration of function ‘date_time_seed’ */
        date_time_seed();
        /*!! foo.c:15: warning: ISO C90 forbids mixed declarations and code */
        int a[50];
        char c;
        int s, i;
    
        printf(" * Please press r/R to run or press q/Q to exit *\n");
        /*!! foo.c:20: warning: implicit declaration of function ‘getch’ */
        c = getch();
        /*run by pressing r and exit by pressing q */
    
        if (c == 'r' || c == 'R') { /*accept both lower and upper case */
            /*move to next statment only if the input in range 1 to 20 */
            do {
                printf("Enter the range size(max=20)");
                scanf("%d", &range);
            }
            while (range < 0 || range > 20);
    
            printf("How many numbers do you wish to generate?");
            scanf("%d", &num);
            count_nums(a, range, num);
            for (i = 0; i < range; i++)
                printf("%d:%d\n", i, a[i]);
            printf("Maximum count:%d\n", get_max(a, range));
            
            printf("Minimum count:%d\n", get_min(a, range));
            printf("Expected: %d\n", (int) (num / range));
        }
        if (c == 'q' || c == 'Q')
            exit(1);
    
        return (0);
    }
    
    int my_rand(int range)
    {
        int r;
        date_time_seed();
        r = 1 + rand() % range;
        return r;
    }
    
    void count_nums(int *a, int range, int num)
    {
        int i;
        for (i = 0; i <= num; i++)
        {
            num = my_rand(range);
            *(a + num) = 0;
        }
    }
    
    /* function to find the max */
    int get_max(int *a, int s)
    {
        int i, max; 
       max= *a;
        for (i = 0; i < s; i++)
            if (*(a + i) > max)
                max = *(a + i);
        return (max);
    }
    
    /*function to find the min */
    int get_min(int *a, int s)
    {
        int i, min;
    
        min = *a;
        for (i = 0; i < s; i++)
            if (*(a + i) < min)
                min = *(a + i);
        return (min);
    }
    This is my latest program. Please help me out. I dont seem to compile it. Please tell me where ive gone wrong and teach me what should i put instead.

    Thanks.
    NiCoLeHa

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int main(void)
    {
        int range, num;
        /*!! foo.c:14: warning: implicit declaration of function ‘date_time_seed’ */
        date_time_seed();
    At this point in your program, you're calling "date_time_seed", and you compiler has no idea where this function is, what it is, or anything about it, so it tells you that.

    Code:
    /*!! foo.c:15: warning: ISO C90 forbids mixed declarations and code */
        int a[50];
        char c;
        int s, i;
    Your compiler is telling you that you're not allowed to call functions, and then after function calls, declare new variables. You need to declare all of your variables at the top of your functions before you call any other functions.

    Code:
        printf(" * Please press r/R to run or press q/Q to exit *\n");
        /*!! foo.c:20: warning: implicit declaration of function ‘getch’ */
        c = getch();
    Your compiler has no 'getch' function, or, if it does, you're not including a header that defines it any place.

    Code:
        /*run by pressing r and exit by pressing q */
    
        if (c == 'r' || c == 'R') { /*accept both lower and upper case */
    Well you don't actually loop or anything here, so while this will do whatever if they press r...
    Code:
        if (c == 'q' || c == 'Q')
            exit(1);
    
        return (0);
    }
    ...it's going to exit here anyway, so the whole 'q' thing is pointless.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Dec 2005
    Posts
    14

    Please help to check

    Question:
    Write a program to show the operation of the random number generator.
    The program will generate a specified number of random numbers in a given range.
    It will count the number of times each number occurs.
    It will display on the screen how many times each number occurred, in the form:
    0 occurred 100 times
    1 occurred 102 times
    2 occurred 98 times
    etc.
    It will calculate the minimum number of occurrences, the maximum number of occurrences and the number of times each number could be expected.
    e.g.
    Minimum count =98, maximum count =102, expected count =100.
    Use date_time_seed() and rand()
    The prpgram will accept the following single keystroke commands from the keyboard ..
    • ‘R’ to run the program, prompting for the number range and the number of numbers to generate. After running it prompts for either R or Q again.
    • ‘Q’ to quit the program.






    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <DBOS\LIB.h>
    #include <CLIB.H>
    
    void count_nums(int *a, int range, int num);
    int my_rand(int);
    int get_max(int *a, int s);
    int get_min(int *a, int s);
    
    main(void)
    {
        int range, num;
        int s, i;
        char c;
       
        date_time_seed();
        
        int a[50], r[50];
       
        
    
        printf(" * Please press r/R to run or press q/Q to exit *\n");
       
        c = getch();
        /*run by pressing r and exit by pressing q */
    
        if (c == 'r' || c == 'R') { /*accept both lower and upper case */
            /*move to next statment only if the input in range 1 to 20 */
            do {
                printf("Enter the range size(max=20)");
                scanf("%d", &range);
            }
            while (range < 0 || range > 20);
    
            printf("How many numbers do you wish to generate?");
            scanf("%d", &num);
            count_nums(a, range, num);
            for (i = 0; i < range; i++){
                printf("%d:%d\n", i,r[i]);
            }
            printf("Maximum count:%d\n", get_max(a, range));
            
            printf("Minimum count:%d\n", get_min(a, range));
            printf("Expected: %d\n", (int) (num / range));
        }
        if (c == 'q' || c == 'Q')
            exit(1);
    
        return (0);
    }
    
    int my_rand(int range)
    {                                                      
    //date_time_seed();
        int i;
        int r[50]; 
        int a[50];
        for( i=0; i<50; i++)
        r[i]=0;
        r[a[i]]=r[a[i]]+1;
        //r = 1 + rand() % range;
        return (0);
    }
    
    void count_nums(int *a, int range, int num)
     { 
       date_time_seed();
        int i;                                                                             
       
        for (i = 0; i <= num; i++){
                                                                                                                                     
            //num = my_rand(range);                                      
           a[i]= 0;                                                                                
            a[i]= rand()% range;                                                                          
           num= my_rand(range);
        
        }
        }
    Do i make any mistake? I so, please tell me. I am handing it in on this Thursday.

    Thanks.
    NiCoLeHa

  6. #21
    Registered User
    Join Date
    Dec 2005
    Posts
    14

    THe whole of my program.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <DBOS\LIB.h>
    #include <CLIB.H>
    
    void count_nums(int *a, int range, int num);
    int my_rand(int);
    int get_max(int *a, int s);
    int get_min(int *a, int s);
    
    main(void)
    {
        int range, num;
        int s, i;
        char c;
       
        date_time_seed();
        
        int a[50], r[50];
       
        
    
        printf(" * Please press r/R to run or press q/Q to exit *\n");
       
        c = getch();
        /*run by pressing r and exit by pressing q */
    
        if (c == 'r' || c == 'R') { /*accept both lower and upper case */
            /*move to next statment only if the input in range 1 to 20 */
            do {
                printf("Enter the range size(max=20)");
                scanf("%d", &range);
            }
            while (range < 0 || range > 20);
    
            printf("How many numbers do you wish to generate?");
            scanf("%d", &num);
            count_nums(a, range, num);
            for (i = 0; i < range; i++){
                printf("%d:%d\n", i,r[i]);
            }
            printf("Maximum count:%d\n", get_max(a, range));
            
            printf("Minimum count:%d\n", get_min(a, range));
            printf("Expected: %d\n", (int) (num / range));
        }
        if (c == 'q' || c == 'Q')
            exit(1);
    
        return (0);
    }
    
    int my_rand(int range)
    {                                                      
    
        int i;
        int r[50]; 
        int a[50];
        for( i=0; i<50; i++)
        r[i]=0;
        r[a[i]]=r[a[i]]+1;
        
        return (0);
    }
    
    void count_nums(int *a, int range, int num)
     { 
       date_time_seed();
        int i;                                                                             
       
        for (i = 0; i <= num; i++){
                                                                                                                                     
                                                  
           a[i]= 0;                                                                                
            a[i]= rand()% range;                                                                          
           num= my_rand(range);
        
        }
        }
        
    
    
    
    /* function to find the max */
    int get_max(int *a, int s)
    {
        int i, max; 
       max= *a;
        for (i = 0; i < s; i++)
            if (*(a + i) > max)
                max = *(a + i);
        return (max);
    }
    
    /*function to find the min */
    int get_min(int *a, int s)
    {
        int i, min;
    
        min = *a;
        for (i = 0; i < s; i++)
            if (*(a + i) <= min)
                min = *(a + i);
        return (min);
    }
    Am i correct? Please help me to check. Tell me and guide me if i've made any mistakes.

    Thanks.
    NiCoLeHa

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 09:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 09:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21