Thread: Issue with random numbers

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    42

    Issue with random numbers

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int xPosCalc(int sigmaMatter, int muMatter, int sigmaAntimatter, int muAntimatter, int xRand)
    {
        xRand = rand() % 36;
        int xPosMatter = (1/(sigmaMatter*sqrt(2*M_PI)))*exp(-((xRand-muMatter)*(xRand-muMatter))/(2*(sigmaMatter*sigmaMatter)));
        int xPosAntimatter = (1/(sigmaAntimatter*sqrt(2*M_PI)))*exp(-((xRand-muAntimatter)*(xRand-muAntimatter))/(2*(sigmaAntimatter*sigmaAntimatter)));
        int xProbability = xPosMatter * xPosAntimatter;
        printf("Random variable is: %d", xRand);
        printf("\nProbability is: %d", xProbability);
    }
    
    int main(void)
    {
        int sigmaMatter = 4.2;
        int muMatter = 27.0;
        int sigmaAntimatter = 6.0;
        int muAntimatter = 17.0;
        int xRand = 0.0;
        xPosCalc(sigmaMatter, muMatter, sigmaAntimatter, muAntimatter, xRand);
    }
    The problem is my random variable (restricted from 0-35) comes out as 29 every time I run the program, not a random number like I need. I'm guessing this is either a problem with the way I'm passing my variables (something I'm still learning) or a stack error.

    I know my math can be simplified, but I don't think that is what's causing the current issue.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you would like to use srand near the start of the main function to seed the pseudorandom number generator, e.g., with a value derived from the current time.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You need to use srand first - Random numbers
    [edit]
    Too slow - I shouldn't have searched for the link!
    [/edit]
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    Rand() is deterministic. It always returns the same sequence of values from the same seed, which is in a defined state at program startup.
    So the normal thing to do is to develop the program, then last thing put a call to srand(time()) at the start of main. Then you can replicate any bugs which appear in development, but the program gives different results each time when run for real.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The numbers are not random, but pseudorandom. See more here, which also has the link of Prelude (for the ones that hang around the forum for much).
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    42
    Is there a faster way to get pseudorandom numbers other than the time() function? For my program I need to calculate 5000 numbers as fast as possible, and if I get the pseudorandom number from time() it will calculate all 5000 numbers based on less than 5000 random numbers, if that makes sense

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    make sure you really want to seed with time(); if you do, your results will not be repeatable and it will be harder to track down problems in your calcs. an alternative to using time(), pass in an argument for the seed so you can make runs with different sets of random values but still have repeatable results.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    505
    The standard way to get random numbers is to call srand(time()) once, then call rand(). time() is rather a slow function, but it's the easiest way to get a seed which varies between runs.
    You don't call time() in a loop, because then the bits will start to go through a regular sequence.
    rand() is good enough for general use, but not especially high quality. It's likely to be a simple linear congruential generator of the form

    Code:
    seed = seed * m + c;
    with an implicit modulus operation. The only way of speeding it up is to go to a smaller seed size.
    You can get better random numbers by using a more sophisticated algorithm.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with Random Number Generation
    By sam.briggs in forum C Programming
    Replies: 3
    Last Post: 10-06-2011, 11:50 AM
  2. Random number issue
    By swgh in forum C++ Programming
    Replies: 15
    Last Post: 11-14-2008, 10:12 AM
  3. Random Number issue
    By SkyRaign in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2005, 01:00 AM
  4. Replies: 4
    Last Post: 11-16-2004, 07:29 AM
  5. Numbers issue...
    By crepincdotcom in forum C Programming
    Replies: 5
    Last Post: 07-21-2004, 03:22 PM