Thread: two questions regarding probability

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    12

    two questions regarding probability

    i have 2 questions, related to each other:

    1. let's say i create a program which the user will be able to input two numbers into. those two numbers would represent ?/? chance of something being created. now let's say the user then inputs the number of seconds the program runs for. once all the data is inputed, the computer works for the number of seconds specificed, and based on the ?/?, a log displays how many things have been created.

    2. if the above question is not very difficult to do, then for every second that the computer is calculating if something is created, can i set it up so that every second a message is displayed whether or not something was created.

    i don't know if what i've said entierely makes sense, but please help anyways, lol.

    thanks in advance,

    jolszewski

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Yes, it is possible to do this.

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    it sounds to me that you are looking to compare theoretical probability against an observed probability of a data set for a given theoretical likelihood. statistics isn't one of my natural strong points, but i recently completed a project on error correcting codes, and as an added feature, i used a concept similar to the one employed in the code below, to determine the characteristics of the decoder when the message had a specific number of errors. without getting all technical, what i had was a large number of messages. i used the encoding algorithm specified to produce the codewords, and then i randomly corrupted a specific number of bits in the codeword. i then proceeded to attempt to decode the original message, and i logged the success rate of the system in that manner. the aim of all this was to experimentally determine the theoretical accuracy of the system when a specific number of bits were corrupted.

    how does this apply here? well the way i see it, you are asking a user to specify a probability of creating a creature. every compiler has the ability to generate pseudo-random numbers, and we'll assume that this is suitable for this current problem. on my system, rand is capable of generating any integer between 0 and RAND_MAX, which in my case is 32767. what the code below does is that it takes in a probability, and divides the range of possible values accordingly. so basically, if i enter in a probability of 0.66 recurring (two thirds), it will set an interval at 21844 - a value two thirds of the maximum possible value. now since we have a capable random number generator, which we assume is even and unbiassed, the direct probability of it generating a number in the range 0 - 21843 is (nearly) twice the probability of it producing a number in the range 21844 to 32767 see?

    so basically, the code does that, then produces a data set by contiuously calling the rand function and observing which side of the interval these values lie. these details are written into a log file. the number of calls to rand will depend on how many seconds you decide to run the program for.

    i think that although i am probably removing most of the hard work, you should have an idea of how it works. hopefully i have understood the requirements of your questions -this program doesnt quite satisfy all of them, just the underlying theory.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main ()
    {
        float numerator;
        float denominator;
        int interval;
        int created = 0;
        int total = 0;
        int end;
        int sample;
        FILE *fp;
        
        srand (time(0));  /*seeds the randome number generator so that 
                          rand returns "good" random values*/
        
        printf ("Enter a numerator: ");
        scanf ("%f", &numerator);
        
        printf ("\nEnter a denominator: ");
        scanf ("%f", &denominator);
        
        printf ("\nThe probability is %f\n\n", numerator/denominator);
    
        /*This following line sets the interval point*/
        interval = (int)((numerator/denominator)*RAND_MAX);
        printf ("This probability sets an interval at %d\n\n", interval);
        
        printf ("Enter how long you wish to run the program: ");
        scanf ("%d", &end);
        
        if ((fp = fopen ("log.txt", "w")) == NULL)
        {
              printf ("\nError: Unable to open file");
              return 1;
        }
        
        end = end + time (0);
        
        while (time (0) < end)  /*this loop writes all data to file*/
        {
              sample = rand ();
              
              fprintf (fp, "Trial: %d\t", total);
              if (sample < interval)
              {
                 fprintf (fp, "Created\n\n");
                 created++;   /*so you will know how many were created*/
              }
                 
              else
                 fprintf (fp, "Not Created\n\n");
              
              total++;        /*so you have the total number of samples*/
        }
        return 0;
    }
    there's still work left to do with this program - it doesn't print any statistics to the screen, and also due to the nature of the loop it uses - it produces loads of values. i ran it for 2 seconds and produced a million samples. also, the nature of the timing is not exact, because it is assumed that it doesnt matter too much. i don't normally write full programs to this degree, but i had some time to kill and it was fun - barring one missing semi-colon and an invalid int declaration the code worked fine! i hope that this will at least give you some idea of tackling your problem if i havent addressed it as required.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

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. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM