programming with random numbers

This is a discussion on programming with random numbers within the C Programming forums, part of the General Programming Boards category; hi i am writting a prgrame to do the following generate a random numbers in a given range count the ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    13

    programming with random numbers

    hi
    i am writting a prgrame to do the following
    generate a random numbers in a given range
    count the occurrences of each number and return the total in arry 'a'

    find the maximum integer in the arry 'a' of size 's'
    find the minimum integer in the arry 'a' of size 's'

    and using function to find max and min

    this is what i did so far to generate the random numbers and but them in an arry 'a' and the range of the random numbers is less than 20 ,, i include a code to run the programme by pressing r and exit by pressing e ,,,,, but it is not working i dunno why i cant fix the errors if someone can help please

    my code ::::

    #include <stdlib.h>
    #include <stdio.h>
    #include <DBOS\LIB.h>
    #include <CLIB.h>
    int main(void)
    {
    int range,num,a;

    int k;

    printf(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    printf(" * Please pres r to run or press e to exit *\n");
    printf(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    k=getch();

    /*run by pressing r and exit by pressing e*/


    if (k=='r' || k=='R'){ /*accept both lower and upper case */
    do{
    printf("Enter the range size(max=20)");
    scanf("%d",&range);
    }
    while (range<0 || range>20); /*move to next statment only if the input in range 1 to 20*/

    printf("How many numbers do you wish to generate?");
    scanf("%d",&num); }
    printf("Expected: %d\n", int (num / range));

    if (k=='e' || k=='E'){
    exit(0);}

    date_time_seed();
    int my_rand(int range){
    int r ;
    r=1+rand()%range;
    }
    void pic(int *a,int range,int num){
    int i,t;
    t=0;

    for(i=0; i<(num); i++)
    a[i]=0;
    for(i=0; i<num; i++)
    {
    t=my_rand(range);
    a[t]=a[t]+1;

    }

    }


    return(0);
    }

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    I fixed up the code and made it so it fills an array 'a' with the random numbers.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    int main(void)
    {
    	int range,num;
    	int a[300];
    
    	int k;
    
    	printf(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    	printf(" * Please pres r to run or press e to exit *\n");
    	printf(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    	k=getchar();
    
    	/*run by pressing r and exit by pressing e*/
    
    
    	if (k=='r' || k=='R'){ /*accept both lower and upper case */
    	do{
    		printf("Enter the range size(max=20)");
    		scanf("%d",&range);
    	}
    	while (range<0 || range>20); /*move to next statment only if the input in range 1 to 20*/
    
    		printf("How many numbers do you wish to generate?");
    		scanf("%d",&num);
    	}
    	printf("Expected: %d\n", (int) (num / range));
    
    	if (k=='e' || k=='E'){
    	exit(0);}
    	pic(a,range,num);
    }
    
    int my_rand(int range){
    	int r ;
    	r=1+rand()%range;
    	return r;
    }
    
    
    void pic(int *a,int range,int num){
    	int i,t;
    	t=0;
    
    	for(i=0; i<(num); i++){
    		a[i]=0;
    		for(i=0; i<num; i++)
    			{
    				t=my_rand(range);
    				a[t]=a[t]+1;
    
    			}
    		}
    	}
    It still needs the rest of the things you mentioned, but try and show some code first.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    13
    thanx alot i will try and do the rest 2moro and post it if i had any problem

    thanx again

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    13
    sorry but i just tried to run the programme and i got an error message :



    function pic has not been declard in the line
    pic(a,range,num);


    i am using a compiler called Salford Plato

  5. #5
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    You could try putting a prototype for the function at the top before main. Like this:
    Code:
    void pic(int*,int,int);
    int my_rand(int);
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    13
    many thanx to sean345 here is my full code but again i
    cant fix the error it will be great help if sean345 or someone else can help me with it


    code:


    #include <stdlib.h>
    #include <stdio.h>
    #include <clib.h>
    #include <DBOS\LIB.h>

    void pic(int*,int,int);
    int my_rand(int);
    int find_max(int *a,int s);
    int find_min(int *a,int s);
    int main(void)
    {
    int range,num;
    int a[300];

    int k,s;

    printf(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    printf(" * Please pres r to run or press e to exit *\n");
    printf(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    k=getch();

    /*run by pressing r and exit by pressing e*/


    if (k=='r' || k=='R'){ /*accept both lower and upper case */
    do{
    printf("Enter the range size(max=20)");
    scanf("%d",&range);
    }
    while (range<0 || range>20); /*move to next statment only if the input in range 1 to 20*/

    printf("How many numbers do you wish to generate?");
    scanf("%d",&num);
    }
    printf("Max count:%d\t",find_max(a,s));
    printf("Minimum cont:%d\t",find_min(a,s));
    printf("Expected: %d\n", (int) (num / range));

    if (k=='e' || k=='E'){
    exit(0);}
    pic(a,range,num);
    }

    int my_rand(int range){
    int r ;
    r=1+rand()%range;
    return r;
    }


    void pic(int *a,int range,int num){
    int i,t;
    t=0;

    for(i=0; i<(num); i++){
    a[i]=0;
    for(i=0; i<num; i++)
    {
    t=my_rand(range);
    a[t]=a[t]+1;

    }

    }
    }
    /* function to find the max*/
    int find_max(int *a,int s){

    int i,max;
    max=0;
    for(i=0;i<s;i++)
    {
    if(a[i]>max)
    max=a[i];
    }
    return max

    }

    /*function to find the min*/
    int find_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;
    }



  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >return max
    .... has a semi-colon missing.

    Please use code tags in future.

    What error messages are you getting?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    13
    thanx i fixed the error but still has a warning says ( s has been used but never given a value) i know that s should be the size of the arry 'a' but i dunno how can i assign a value for s . also when i run my programe i got a small error window saying
    access violation the instraction to address 00401216 attampted to read from location 03713000


    here is my code

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <clib.h> 
    #include <DBOS\LIB.h> 
    
    void pic(int*,int,int);
    int my_rand(int); 
    int find_max(int *a,int s);
    int find_min(int *a,int s);
    int main(void)
    {
            int range,num;
            int a[300];
    
            int k,s;
    
            printf(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
            printf(" * Please pres r to run or press e to exit *\n");
            printf(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    k=getch();
    
            /*run by pressing r and exit by pressing e*/
    
    
            if (k=='r' || k=='R'){ /*accept both lower and upper case */
            do{
                    printf("Enter the range size(max=20)");
                    scanf("%d",&range);
            }
            while (range<0 || range>20); /*move to next statment only if the input in range 1 to 20*/
    
                    printf("How many numbers do you wish to generate?");
                    scanf("%d",&num);
            }  
                    printf("Max count:%d\t",find_max(a,s));  
                    printf("Minimum cont:%d\t",find_min(a,s));
                    printf("Expected: %d\n", (int) (num / range));
    
            if (k=='e' || k=='E')
            exit(1);  
            
            pic(a,range,num);
    }
    
    int my_rand(int range){
            int r ;
            date_time_seed();
            r=1+rand()%range;
            return r;
    }
    
    
    void pic(int *a,int range,int num){
            int i,t;
            t=0;
    
            for(i=0; i<(num); i++){
                    a[i]=0;
                    for(i=0; i<num; i++)
                            {
                                    t=my_rand(range);
                                    a[t]=a[t]+1;
    
                            }
    
              }
              }  
              /* function to find the max*/
      int find_max(int *a,int s){
    
            int i,max;
            max=0;
            for(i=0;i<s;i++) 
                    {
                    if(a[i]>max)
                            max=a[i];
                            }
            return max;
                
          
              }  
                
             /*function to find the min*/
        int find_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;
              }
    Last edited by xstudent; 05-20-2002 at 08:51 AM.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This is what I get
    Code:
    1 - Function should return a value in function main
    2 - Call to function 'date_time_seed' with no prototype in function my_rand
    3 - 't' is assigned a value that is never used in function pic
    1 - Put a return (0); statement at the end of main().

    2 - Where is function date_time_seed();

    3 - is a warning only, because you did "t = 0;" without using t before assigning it a new value.

    I had to change the header files, I don't have clib.h or DBOS/LIB.h, and I had to use conio.h for getch().

    When I run it
    Code:
     *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
     * Please pres r to run or press e to exit *
     *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    Enter the range size(max=20)20
    How many numbers do you wish to generate?10
    Max count:0     Minimum cont:0  Expected: 0
    Without reading all your code, I am not sure what output you are expecting, but all zeros seems a bit strange to me! I'll leave that for you to debug once you get it compiled.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    13
    the output shold be the number of times that the maximum occures and the number of times that the minimum occures in the random numbers generated but i am getting zeros all the time ??

    can anyone help

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It looks to me that your function pic(a, range, num); is used to setup the random numbers in the array?

    First off, the array is uninitialised when you start to use it, which is wrong. You are doing
    >a[t] = a[t] + 1;
    when you have no idea what a[t] has in it. Arrays of this nature do not start life set to zero.
    I presume you are trying to zero everything here
    >for(i=0; i<(num); i++){
    > a[i]=0;
    but because of the loop structure, it isn't working.


    Now when you call this function:
    >printf("Max count:%d\t", find_max(a, s));
    where did s come from? it hasn't got a value either. I presume you meant to pass num?

    Have a go at fixing, post again when you have more problems.
    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 2002
    Posts
    13
    i tried to do this
    >
    Code:
     find_max(a,num)
    using num not s this time

    also
    Code:
      for(i=0; i<(num); i++){
                    a[i]=0;}
                    for(i=0; i<num; i++)
                            {
                                    t=my_rand(range);
                                    a[t]=a[i]+1;
    
                            }

    so that each element in my arry a[t] is the zero element incremented by one each time the number occure in the random set

    but still gettint the zero output as before

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Ok, I think we wanted
    Code:
    find_max(a,range)
    (Got confused between num and range!)

    Also, if you add an int i to the top of main() and add this to end of main()
    Code:
    for (i = 0; i < range; i++) printf ("%d:%d\n", i, a[i]);
    you will get to see what's in the array. That'll help you understand whats going on.

    One immediate problem I noticed was this:
    Code:
    Enter the range size(max=20)15
    How many numbers do you wish to generate?99
    Max count:11    Minimum cont:0  Expected: 6
    0:0  <--- This will always be so, because of your code.
    1:3
    2:10
    3:5
    4:4
    5:9
    6:9
    7:9
    8:4
    9:8
    10:5
    11:11
    12:4
    13:9
    14:4
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    May 2002
    Posts
    13
    my code dosent even generate the random numbers coz this is what i got when i added the line
    Code:
    for (i = 0; i < range; i++) printf ("%d:%d\n", i, a[i]);
    0:0
    1:0
    2:0
    3:0
    4:0
    ..

    .all zero arry i dunno whats wrong ............

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, 09:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-12-2003, 12:03 AM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM

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