CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    May 2010
    Posts
    6

    How to generate random numbers without using rand() function?

    In a statistics book there is an algorithm of generating pseudo-random numbers with uniform distribution [0,1]. It says:
    I. Enter an initial variable X positive integer.
    II. Multiply X by variable "a", which should be at least 5 digits long.
    III. Divide a*X by value p (positive integer).
    IV. Take the fraction part of the division as a first pseudo-random number.
    V. Make the number from step IV into integer by a necessary multiplication and use it in step II.
    VI. Step II-V are repeated to generate more pseudo-random numbers.

    Here is what I have:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int x,a,p;
        cout << "Enter initial positive integer number: ";
        cin >> x;
        cout << "Enter positive integer a: ";
        cin >> a;
        cout << "Enter positive integer p: ";
        cin >> p;
        for(int i=1; i<=12; i++)
        {
            x=(a*x % p);
            cout << x << endl;
        }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    How do I realize steps 4 and 5? I need to obtain random numbers between 0 and 1 and later use them in some other calculations. Please, help.

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: How to generate random numbers without using rand() function?

    This sounds very repeatable. I don't even think that algorithm could be considered pseudo-random. Sounds to me that given the same x, a, and p, you will get the same resulting value.

    The only way to do random numbers without rand is to write your own rand function. The clock tick and a hash is the only way to do this across platforms. If you know what platform you will be using, you can do some more things that will generated more random number, some algorithms use the temperature of the processor to produce random numbers.

    If you are hellbent on using that algorithm, then you do step 4 by multiplying your floating point by (float)INT_MAX. Step three will give you a value between 0 and 1, so to go to integer, just multiply by the maximum integer. Then truncate.

    If you really want, I can give you a better random number generator, but it does some tricky stuff. And it's a bit of overkill.
    Last edited by ninja9578; January 16th, 2011 at 02:03 PM.

  3. #3
    Join Date
    May 2010
    Posts
    6

    Re: How to generate random numbers without using rand() function?

    Well, yes, we can dump this thing. Then, how can I get a decimal number between 0 and 1 and store these numbers for some other calculations?
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "ccc_time.h"
    #include <cmath>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        Time now;
        int seed=now.seconds_from(Time(0,0,0));
        srand(seed);
        double r;
        for (int i=1; i<=10; i++)
        {
            r=(rand() &#37; what to place here?);
            cout << r << endl;
        }
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: How to generate random numbers without using rand() function?

    double d = (double)rand() / (double)MAX_RAND;

  5. #5
    Join Date
    May 2010
    Posts
    6

    Re: How to generate random numbers without using rand() function?

    `MAX_RAND' undeclared (first use this function)

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: How to generate random numbers without using rand() function?

    Code:
    #ifndef MAX_RAND
       #ifdef RANDOM_MAX
          #define MAX_RAND RANDOM_MAX
       #else
          #define MAX_RAND 0x7FFFFFF
       #endif
    #endif
    MAX_RAND is required to be defined by both BSD and C++ standards. If it's not defined, I highly recommend getting a compiler with a library that adheres to the proper standards. May I ask what compiler you are using? Some embedded compilers do not adhere to all of the standards for certain memory and power constraints, but all desktop based compilers should.

  7. #7
    Join Date
    May 2010
    Posts
    6

    Re: How to generate random numbers without using rand() function?

    Dev-C++ 4.9.9.2

  8. #8
    Join Date
    Jan 2009
    Posts
    1,689

    Re: How to generate random numbers without using rand() function?

    MinGW? MY guess is it's a very old version of MinGW, and it might not have been fully compliant when Dev-C++ stopped being developed. Dev-C++ hasn't been worked in in nearly a decade, it became Code::Blocks. I recommend upgrading.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to generate random numbers without using rand() function?

    Quote Originally Posted by ninja9578 View Post
    This sounds very repeatable. I don't even think that algorithm could be considered pseudo-random. Sounds to me that given the same x, a, and p, you will get the same resulting value.
    True, but the same is true of the rand() function so this isn't a limitation. (Same srand() will produce the same subsequent series of rand() values.)

    MAX_RAND is required to be defined by both BSD and C++ standards.
    You're thinking of RAND_MAX, not MAX_RAND.

  10. #10
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: How to generate random numbers without using rand() function?

    Quote Originally Posted by ninja9578 View Post
    This sounds very repeatable. I don't even think that algorithm could be considered pseudo-random. Sounds to me that given the same x, a, and p, you will get the same resulting value
    Random numbers and repeatable isn't a contradiction. As Lindley said the rand() function also is repeatable, i. e. it always give the same numbers, when using the same seed number. The 'random' property it gets because the probability of each positive integer (up to the maximum) to arise as next number is nearly the same given you were using a random seed (e. g. derived from clock ticks).

    Repeatable random series are necessary for some encoding/decoding algorithms where the seed number is the key and the random property of the series guarantees that you can't deduce the seed number from the encoded data beside of doing a brute-force.

  11. #11
    Join Date
    Jan 2009
    Posts
    1,689

    Re: How to generate random numbers without using rand() function?

    But it's not predictable. Normally you seed the randomizer with the current clock tick, so every time it runs, you will get a different set of integers.

    Running
    Code:
    srand(time(NULL));
    printf("&#37;d, %d, %d\n", rand(), rand(), rand());
    Should give you different results each time you run it. Not true with 3 input number, granted, if he used time(NULL) for each value, it would be "random", but that's not what he's doing.

  12. #12
    Join Date
    May 2010
    Posts
    6

    Re: How to generate random numbers without using rand() function?

    It's too complicated and stupid anyway. Let's dump it.

  13. #13
    Join Date
    Nov 2003
    Posts
    1,902

    Re: How to generate random numbers without using rand() function?


  14. #14
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to generate random numbers without using rand() function?

    Quote Originally Posted by ninja9578 View Post
    But it's not predictable. Normally you seed the randomizer with the current clock tick, so every time it runs, you will get a different set of integers.
    And given similarly selected seed values for x, a, and p, this algorithm should in theory generate unpredictable numbers too.

    Of course, "normally" the srand() call is only added after the program is fully debugged, since it is desirable to obtain repeatable sequences during debugging.

  15. #15
    Join Date
    May 2009
    Posts
    2,413

    Re: How to generate random numbers without using rand() function?

    Quote Originally Posted by Codeplug View Post
    Thanks for the link. I've been looking for a better way to generate a random seed from the system clock.

    But I think it's time to leave the old rand() behind and go for the new improved random facilities of C++0x. One can easily switch between different random distributions and ranges, and there's a choise of several random engines. I'm using it like this,

    Code:
    #include <random>
    #include <functional>
    #include <ctime>
    
    unsigned long time_seed() {
    	time_t now = time(0);
    	unsigned char *p = (unsigned char *)&now;
    	unsigned long seed = 0;
    	size_t i;
    	for (i=0; i<sizeof now; ++i )
    		seed = seed * (UCHAR_MAX + 2U ) + p[i];
    	return seed;
    }
    
    std::uniform_real_distribution<double> distribution(0.0,1.0);
    std::mt19937 engine(time_seed());
    //std::minstd_rand0 engine(time_seed());
    auto generator = std::bind(distribution, engine);
    
    double rnd() { // uniformly distributed random double in the 0.0 to (but not including) 1.0 range
    	return generator();
    }
    std::mt19937 is the famous Mersenne twister engine. std::minstd_rand0 is a linear congruent engine, very much like the one rand() uses. If one wants a fixed seed it's just to replace the call to time_seed() with a constant like say 13U.
    Last edited by nuzzle; January 19th, 2011 at 01:06 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured