Random numbers help

This is a discussion on Random numbers help within the C Programming forums, part of the General Programming Boards category; Hello, I trying to write a program using the probability function to create 2 files. One of them will generate ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    13

    Random numbers help

    Hello,

    I trying to write a program using the probability function to create 2 files. One of them will generate 100 random numbers and the other a 1000. The average is supposed to be 0.5.

    This is what I have so far.

    Code:
    #include <sdtio.h>
    #include <math.h>
    
    #define INITIAL_SEED		17
    #define MULTIPLIER		25173
    #define INCREMENT		13849
    #define MODULUS			65536
    #define FLOATING_MODULUS	65536.0
    
    static unsigned seed = INITIAL_SEED;
    
    unsigned random(void)
    {
    	seed = (MULTIPLIER * seed + INCREMENT) % MODULUS;
    	return seed;
    }
    
    double probability(void)
    {
    	seed = (MULTIPLIER * seed + INCREMENT) % MODULUS;
    	return (seed / FLOATING_MODULUS);
    }
    Am I going in the right direction? Any help will be appreciated.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Go up a block and turn left.
    You can't miss it.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Let's say you had a function that randomly would give you a value between 0 and 1. As the number of random values is increased, wouldn't the average of those values, be very close to 0.5?

    Isn't that just what C's random functions can do?

    I believe you've over thought your program, or am I not right? I'm reminded of a marketing assignment that was given to a hot shot Ivy League, aimed at finding just THE right price for a new companies product.

    While one study group tediously worked up from all kinds of details (cost of raw product, cost of manufacturing, transportation, etc.), another study group had their answer in 5 minutes: they walked in and priced their competitors version of the product, and set it at the same level. They had figured quite correctly that they couldn't compete if they charged more, especially since the company was new, and they had no reason to charge less, either.

    I'm thinking that's something similar to what's happening here. All these number machinations are unnecessary if the random distribution converges on 0.5, anyway.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    Your probably right....any help in getting this settled? I am so lost at doing this right now.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I can't help because I have no idea just what your assignment was, (if you stated it all), and where you got these numbers from and what you're really doing with them.

    Do you have an equation for this that you need to use from your teacher? Does it have a name or description? I'm at a loss to figure WTF you're doing here, but I'm no statistician, either.

    What is the output of your program, as it stands now? You didn't include the rest of the program, so I can't run it.

    C'mon, fess up!
    Last edited by Adak; 01-31-2008 at 07:19 PM.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    That program I gave is the example the book gave for random and probability.

    This is my assignment word for word:

    "Use the function probability() to create 2 files, one with 100 random numbers and the other with 1000 random numbers. All numbers will lie in the range from 0 to 1. The average of the files must be 0.5."

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    What does it say probability should do?

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    13
    The same way it is used the the code i provided.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It has the form of a linear congruential RNG, but I don't think any of us (who have posted yet so far) are ready or willing to verify that it actually produces a reasonable pseudo-random distribution. If you know that it does, then fine -- call it 100 times and write the answers to a file. Then call it 1000 times and write the answers to a file. (I'm more-than-a-little worried about the average supposed to be exactly 0.5, though, since that's not really supposed to happen in a true PRNG.)

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, now I'm starting to get the picture, here.

    So you HAVE to use that probability function, that's settled.

    It's the rest of the program that you're having a problem with, then.

    Let's start with some rough logic:

    Code:
    /* add your function prototypes here */
    
    
    int main(void)
    {
       FILE = fp1                            /* a file pointer for output */
       
       for(i = 0; i < 2; i++)              /* two files so an outer loop of 2 values */
       {
           if( i == 0)  
           {                         
               VarNumber = 100; 
                fopen(100Num.txt, wt)      /* open the 100 number file for writing text */
           }
           else
           {
               close the first file here.
               VarNumber = 1000             
               fopen(1000Num.txt, wt)       /* open the 1000 number file "    "        "*/
           }   
           for(j = 0; j < VarNumber; j++)
           {
               r_number = probability()      /*return random number, here*/
               fprintf( print out r_number to the fp1 file stream)  /*and print it to file, here*/
           
           }
       }
        close the second file, here.
        
    
        return 0;
    }
    And none of this is final code - it's just some idea's for how I think it could go. Fleshing out these details will give you a better understanding of how C works.

    This is how I program, btw. First, idea's on a rough draft, then flesh it out and put off the details until later. Then insert all the details after the rough draft is in place, and don't be afraid to re-arrange the rough idea's, all around. Think of those idea's as "scaffolding" for your program.

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