|
-
January 16th, 2011, 01:17 PM
#1
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.
-
January 16th, 2011, 01:59 PM
#2
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.
-
January 16th, 2011, 02:08 PM
#3
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() % what to place here?);
cout << r << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
-
January 16th, 2011, 03:22 PM
#4
Re: How to generate random numbers without using rand() function?
double d = (double)rand() / (double)MAX_RAND;
-
January 16th, 2011, 03:29 PM
#5
Re: How to generate random numbers without using rand() function?
`MAX_RAND' undeclared (first use this function)
-
January 16th, 2011, 04:33 PM
#6
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.
-
January 16th, 2011, 05:19 PM
#7
Re: How to generate random numbers without using rand() function?
-
January 16th, 2011, 06:53 PM
#8
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.
-
January 16th, 2011, 09:28 PM
#9
Re: How to generate random numbers without using rand() function?
 Originally Posted by ninja9578
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.
-
January 18th, 2011, 02:40 PM
#10
Re: How to generate random numbers without using rand() function?
 Originally Posted by ninja9578
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.
-
January 18th, 2011, 03:29 PM
#11
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("%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.
-
January 18th, 2011, 03:38 PM
#12
Re: How to generate random numbers without using rand() function?
It's too complicated and stupid anyway. Let's dump it.
-
January 18th, 2011, 04:09 PM
#13
Re: How to generate random numbers without using rand() function?
-
January 18th, 2011, 04:16 PM
#14
Re: How to generate random numbers without using rand() function?
 Originally Posted by ninja9578
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.
-
January 19th, 2011, 12:35 AM
#15
Re: How to generate random numbers without using rand() function?
 Originally Posted by Codeplug
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|