Issue with Random Number Generation

This is a discussion on Issue with Random Number Generation within the C Programming forums, part of the General Programming Boards category; Hey everyone, Having some problems with generating a random number w. I'm using an algorithm to create w by generating ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    11

    Issue with Random Number Generation

    Hey everyone,

    Having some problems with generating a random number w. I'm using an algorithm to create w by generating two numbers between -1 and 1 and then squaring them and then summing then and then using user in putted means and standard deviations to generate tailor made distributions of random numbers. Here it is: NO code please just advice on what I'm doing wrong as if you run it it never produces sensible answers for w:



    Thanks once again guys! XD
    Last edited by sam.briggs; 10-06-2011 at 12:33 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What would you consider a "sensible answer" for w?

  3. #3
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by sam.briggs View Post
    Hey everyone,

    Having some problems with generating a random number w. I'm using an algorithm to create w by generating two numbers between -1 and 1 and then squaring them and then summing then and then using user in putted means and standard deviations to generate tailor made distributions of random numbers. Here it is: NO code please just advice on what I'm doing wrong as if you run it it never produces sensible answers for w:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
        double x, y, z, m, s, w;                                                            //Define set of doubles for data storgae
        int i, N;                                                                              //Define Integer for counting purposes
    
    printf("Please input the standard deviation you require:");                              //Acquire sigma data
    scanf("%f",&s);
    printf("\nPlease input the mean value you require from the Gaussian distribution:");    //Acquire mu data
    scanf("%f",&m);
    printf("\nPlease state the number of random variables you would like printed:");
    scanf("%d",&N);
    
        for(i=0; i<N; i++)                                                                 //Generate random variable z between 0 and 1
        {
            x = 2* rand()/(double)RAND_MAX -1.0;
            y = 2* rand()/(double)RAND_MAX -1.0;
            z = (x*x) + (y*y);
    
            if(z<1)
            {
                w = ((s*x*sqrt((-2.0*log(z))/(double)z))+m);
                printf("x=%f, y=%f, z=%f, w=%f\n",x,y,z,w);
            }
            else
            {
                printf("Z is invalid=%f, x=%f, y=%f\n",z,x,y);
            }
        }
    
    return 0;
    }
    Thanks once again guys! XD
    The '%f' format specifier in scanf expects the address of a float, not a double. Fix that first and then come back if it still doesn't work as expected.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    11
    Cheers guys that sorted it out! XD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number generation
    By cheeta in forum C Programming
    Replies: 2
    Last Post: 04-29-2010, 11:53 PM
  2. Replies: 5
    Last Post: 10-05-2009, 10:21 AM
  3. random number generation
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-26-2007, 04:51 AM
  4. Random Number Generation
    By drdroid in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 08-02-2003, 03:35 AM
  5. Random number generation
    By Lisa Mowbray in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2002, 12:22 PM

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