GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-May-2007, 22:23
feicobain feicobain is offline
New Member
 
Join Date: Feb 2007
Posts: 10
feicobain is an unknown quantity at this point

Adopt a function that generate random values to a program


Hey guys:
I am writting a program to analysis beam deflection with random beam stiffness. I have written most of it except that I need to adopt a function what will generate random stiffness values. I really have no idea how to put it into my program. I have tseted it with a simple multipication program but it doesn't compile pleae give me some advice thanks.

CPP / C++ / C Code:
#include<math.h>
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

      float T;
      int main

{
        union  { unsigned int i[2]; time_t d;} t;
        t.d=time(NULL);
        srand(t.i[0]);
       
       RandInvNormal(double mean, double stdev);
       
       T=3*mean;
       printf("T=%f\n",T);


}



double RandInvNormal(double mean, double stdev)
{/* Inverse Normal cumulative distribution function.
   FUNCTION PROVIDED TO STUDENTS FROM MODULE WEB-SITE.
   Returns a "random" value from a Normal distribution with given "mean" and "stdev"

   Adapted from [url]http://home.online.no/~pjacklam/notes/invnorm/[/url]
   Note: The following libraries are needed:
        #include <stdlib.h>
        #include <time.h>
   Start of main function should include following code to "seed" random number generator:
        union  { unsigned int i[2]; time_t d;} t;
        t.d=time(NULL);
        srand(t.i[0]);
*/

{
        // Define constants to curve fit for integral
        const double A1 = -3.969683028665376e+01;
        const double A2 = 2.209460984245205e+02;
        const double A3 = -2.759285104469687e+02;
        const double A4 =  1.383577518672690e+02;
        const double A5 = -3.066479806614716e+01;
        const double A6 =  2.506628277459239e+00;

        const double B1 = -5.447609879822406e+01;
        const double B2 =  1.615858368580409e+02;
        const double B3 = -1.556989798598866e+02;
        const double B4 =  6.680131188771972e+01;
        const double B5 = -1.328068155288572e+01;

        const double C1 = -7.784894002430293e-03;
        const double C2 = -3.223964580411365e-01;
        const double C3 = -2.400758277161838e+00;
        const double C4 = -2.549732539343734e+00;
        const double C5 = 4.374664141464968e+00;
        const double C6 = 2.938163982698783e+00;

        const double D1 = 7.784695709041462e-03;
        const double D2 = 3.224671290700398e-01;
        const double D3 = 2.445134137142996e+00;
        const double D4 = 3.754408661907416e+00;

        const double P_LOW = 0.02425;
        const double P_HIGH = 0.97575;  // P_high = 1 - p_low

        double p, q, r, x;

        do
        {
           p = (float)(rand())/RAND_MAX;     // Get a random number between 0 and 1



                                                // rand() returns an int between 0 and RAND_MAX
        } while (p <= 0 || p >= 1);             // Repeat making sure p is not 0 or 1

        if (p > 0 && p < P_LOW)
        {
                q = sqrt(-2*log(p));
                x = (((((C1*q+C2)*q+C3)*q+C4)*q+C5)*q+C6) / ((((D1*q+D2)*q+D3)*q+D4)*q+1);
        } else if (p >= P_LOW && p <= P_HIGH)
        {
                q = p - 0.5;
                r = q*q;
                x = (((((A1*r+A2)*r+A3)*r+A4)*r+A5)*r+A6)*q /(((((B1*r+B2)*r+B3)*r+B4)*r+B5)*r+1);
        } else {
                q = sqrt(-2*log(1-p));
                x = -(((((C1*q+C2)*q+C3)*q+C4)*q+C5)*q+C6) / ((((D1*q+D2)*q+D3)*q+D4)*q+1);
        }

        return (mean + x*stdev);
}
}
Last edited by admin : 02-May-2007 at 23:27. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 02-May-2007, 23:11
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Adopt a function that generate random values to a program


You've been asked to use code tags at least 5 times, and read the Guidelines at least twice. Do you have a problem following suggestions? If so, programming is not a good area for you...
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #3  
Old 02-May-2007, 23:25
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,641
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Adopt a function that generate random values to a program


Quote:
Originally Posted by feicobain
Hey guys:
t I need to adopt a function what will generate random stiffness values. I really have no idea how to put it into my program.

Here's what I think I see:

1. You are given a function double RandInvNormal(double mean, double stdev).

2. You call the function with values of mean and stdev, and the function returns a number that somehow represents a sample from a random process with the specified statistical properties.

3. The function calls the library function rand() and returns a value.

4. You want to write a main() function that calls the function in such a way as to test it.

You are also given a way to "seed" the library function rand() according to system time.

A simple test for starters might be to generate a sequence of numbers and test the mean and the standard deviation of the sample set. That is not necessarily adequate, since it wouldn't actually tell you anything about how closely the distribution of the numbers would match a set of numbers that came from a "real" random process with the specified statistical properties, but it's a start.

Here's the skeleton of the program:

1. Define values of mean and standard deviation of the desired samples.

2. Seed the library function rand()

3. Make a loop that calls RndInvNormal() a lot of times and accumulates the returned values in such a way that you can calculate the sample mean and sample standard deviation of the sequence of numbers returned by the function.


For starters, maybe you just want to call the function 20 times and print out the results. Look at the numbers that it gives you and decide whether they are a reasonable set of sample values. Run the program several times and see if the numbers look "OK".

Here's the skeleton of the program:

Headers for functions that you will use

Function prototype declaration for RandInvNormal()

The main function

Declare variables.
Seed the library rand() function
Make a loop that calls RandInvNormal() a bunch of times and print out the result each time

The RandInvNormal function--- exactly as given

Here is a program that calls the function one time and prints out the result:
CPP / C++ / C Code:
/* the headers */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

/* function prototype */
double RandInvNormal(double mean, double stdev);

int main()
{
    union {
        unsigned int i[2];
        time_t d;
    } t;
    double mean, stdev;
    double x;

    t.d = time(NULL);
    srand(t.i[0]);

    mean = 1.0;
    stdev = 1.0;

    /* put the next two statements in a loop if you want */
    x = RandInvNormal(mean, stdev);
    printf("x = %f\n", x);

    return 0;
}

/* now put the code for RandInvNormal() here: */
double RandInvNormal(double mean, double stdev)
{
    /* Define constants to curve fit for integral */
    const double A1 = -3.969683028665376e+01;
    const double A2 = 2.209460984245205e+02;
.
.
.
.
}

Now, the value of the seed for rand() comes from system time, which changes once per second, so if you run this program several times at least a second apart, you will get numbers from the function. Better yet, make a loop that has only the function call statement and print statement, so that a single program run will give you as many values as you want.
I gave it a mean of 1.0 and a standard deviation of 1.0. You give it whatever you want.

Values from a few runs:

Code:
x = 1.460092e+00 x = 1.640002e+00 x = 1.941322e+00 x = 1.701927e-01 x = 1.071489e+00 x = 1.027766e+00 x = 9.224942e-01 x = 1.983664e+00 x = 2.379336e+00 x = 1.012119e+00 x = 7.288133e-01 x = -2.708088e-01 x = 1.876923e+00 x = 1.030313e+00 x = 5.578928e-01 x = 1.644104e+00 x = 2.447824e+00 x = -4.640906e-01 x = 5.173548e-01 x = 5.356703e-01

If you get a set of numbers that "seem to be OK", then maybe you will want to make more sophisticated tests of the "randomness" properties (or, maybe, not).

Regards,

Dave
 
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
floating point decimal to ascii conversion crazypal C Programming Language 5 18-Apr-2007 04:59
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
Nested for loop with function Tori CPP / C++ Forum 11 08-Nov-2004 13:02
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:31.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.