Thread: calculating the variance of random numbers

  1. #1
    Unregistered
    Guest

    Question calculating the variance of random numbers

    My assignment was to create a 10 by 10 table that stores 100 random numbers between 0 and 99,999. Then calculate the mean, variance and standard deviation of the numbers.

    I have created the table and calculated the mean, but I'm having trouble on how I might calculate the variance of the random numbers. Also, when I run the program, the mean is usually between 0 and 400. This doesn't seem right, shouldn't the mean be somewhere in the 5 digit range?

    This is my code so far.

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

    int main()
    {
    long int seed;
    int i;
    double sum;
    double mean;

    printf("Enter an integer number: ");
    scanf("%ld",&seed);
    srand(seed);

    printf("\n\n\tResults of Programming Assignment 4 (Buisiness Option)\n");
    printf("\t\tList of 100 Randomly Generated Numbers\n\n");
    for(i=0;i<100;i++) printf("%5d\t",rand());
    sum += rand();
    mean=sum/100;

    printf("\n\nArithmetic Mean : %g", mean);
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Weeeeel, a few thoughts:

    rand() returns a random int between 0 and RAND_MAX -- RAND_MAX is usually 0x7FFF or 32,767, so it's NOT going to span the range you're looking for. You might try (rand() * rand())%100000.

    You also aren't correctly averaging ANYTHING. Each loop, you should be storing a random number, printing it, and adding it to a running sum. You never store the random number, sum is never initialized, and sum is only added to once, not once per loop.

    And, unless you actually do create a table in memory (i.e. an array) you'll never be able to calculate variance.

  3. #3
    Unregistered
    Guest
    How would you store random numbers?

  4. #4
    Unregistered
    Guest
    The V~

    How would you store the random number and add it to the running sum?

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Use an array.

    You want to store 100 ints?

    int array[100];

    then array[0] through array[99] are int variables. You could fill the array like this:

    for (i = 0; i < 100; i++){
    array[i] = (rand() <<15 | rand())%100000;
    }

    Now, each array element has a random int between 0 aND 99,999. Don't worry TOO much about what (rand() <<15 | rand()) is all about, it's to take into consideration that rand only randomizes 15 bits (too few for this problem) so we do it twice and shift to get 30 bits randomized.

    To get the mean, add all of them together into a double variable (you can do this inside the array creation loop, or do it as another loop), and divide by 100.

  6. #6
    Unregistered
    Guest
    actually, there IS a way to calculate the variance without using an array. I had a similar assignment back in school and there IS a formula out there to calculate variance without storing each value. I'm 100% sure of this, but unfortunately, I can't prove it because I can't remember the formula. HOWEVER, this 'variance' that I'm talking about will not give the same result as the variance that requires an array. There are actually 2 different mathematical formulas for finding variance and they result in 2 different answers. Sorry I can't back it up with the formula but it is true.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2
    My revised code is below. I tryed using an array and it runs but i'm not sure it's doing anything.

    I now need to add up the random numbers in the array. I've struggled with this for awhile now and still can't figure it out. Can anyone help me out with this.



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

    int main()

    {
    unsigned int i;

    printf("Enter Integer Number: ");
    scanf("%d",&i);

    srand(i);
    for(i=0; i<=99; i++) {
    printf("%5d\t",array[i]=rand()*rand()%100000);
    }
    }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It runs? Really? Without declaring 'array', it runs?

    Ok, so as your code shows, you know how to loop through each cell in an array. You're telling me that you know how to look at, and even assign values to an array cell, but you can't figure out how to loop through an array and add numbers?

    Quzah.

  9. #9
    Unregistered
    Guest
    by the way the previous post was mine

  10. #10
    Unregistered
    Guest
    Quzah~

    Actually i can't. I have no idea about most everything in C programming. I got my code from 100s of examples on-line and i pieced it together.

  11. #11
    Unregistered
    Guest
    Oh wait i did declare the array. I forgot to post that. My code is in the computer lab and i am at home trying to remeber it. Sorry about that.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Quzah~
    >
    > Actually i can't. I have no idea about most everything in C
    > programming. I got my code from 100s of examples on-line and
    > i pieced it together.

    Ok, that would definately make it difficult then, wouldn't it?

    An 'array' is a list of a specific data type. Here's an example:

    int myArray[20];

    This means we declare a variable, of type 'int', called 'myArray', that is a list of 20 integers.

    To access a single instance in that list, to either get a value from it, or give a value to it, you use [] markers. Like so:

    x = myArray[5]; //get a value from cell 5 and put it in 'x'.

    myArray[5] = 20; //put the value of 20 into cell 5.

    Now, while I say 'cell 5', it's actually the 6th in the list, because with arrays, you begin counting from 0. This means that valid entrys for this array are 0 through 19. If you go outside of that, you're corrupt some memory and break things.

    Ok, so now then, with your for() loop, you can count up through the array, one cell at a time:
    Code:
    for( x = 0; x < 20; x++ )
       myArray[x] = x + 13; //put the current value of 'x', plus 13, here.
    You could do the same thing to total up the value of each cell in the array by instead of putting a value into the array, extracting a value from it. Do this exactly like I did it, except switch it around:

    total = total + myArray[x];

    (Note: you can use '+=' as a shortcut: total += myArray[x]

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

  13. #13
    Unregistered
    Guest
    Thanks for your help

    I think I got how to add the values of the array together and find the average, but i need some more help.

    I am now trying to find how to calculate the variance. For the variance you're first suppose to find each individual value minus the mean and square it. I know how to do this the long way by finding the difference between each value and the mean 100 times, but is there an easier way to do this? Can you find the differences in the individual values and mean all at once for the 100 random numbers?

  14. #14
    Unregistered
    Guest
    Will either of these work for calculating the variance?


    variance=(variance+a[100]-mean)**2
    variance=variance/(n-1)

    or

    sum_of_squared += a[100]*a[100]
    variance=sum_of_squares/n-mean*mean

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Sorry, but you're going to have to do the subtract mean then square process for each value. It really isn't a big deal though.
    Code:
    for(i = 0; i < 100; i++) variance += (a[i] - mean)*(a[i] - mean);
    variance /= 99;
    Callou collei we'll code the way
    Of prime numbers and pings!

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, 10:47 PM
  3. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 01:22 PM
  4. random numbers
    By h_howee in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2005, 02:56 PM
  5. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM