How to generate a random number in ATS? - ats

For instance, what is the name of the function that returns a randomly generated floating point number between 0 and 1, left inclusive and right exclusive?

You can use the drand48 function from the standard library. See this snippet for a working example.

Related

How can I use the float version of nfp()?

I'm trying to use the nfp() function in Processing to present my floats on screen with a certain number of decimal places. In the manual page for this function, it says that
There are two versions: one for formatting floats, and one for formatting ints.
However, when I'm trying to use the function with a float variable (and an int variable for the number of decimal points), I get the following error:
The function "nfp()" expects parameters like; "nfp(int,int)".
Am I missing something here? How can I access the float version of the function?
The nfp function formats numbers into strings and adds zeros to it.
This is done for integers always before the given number, which is why the function npf(int, int) requires only one more parameter for the digits.
The function nfp(float, int) does not work. The function requires for a float input two integers: nfp(float, int, int).
This is, since it needs to know, how many digits will be added before the dot (left) and how many should be added after the dot (right).
nfp(1.2, 1, 2) will lead to +1.20
nfp(1.2, 2, 1) will lead to +01.2
Not a big issue? NO! This is a perfect example to learn two things:
It is important that thrown Errors have to make clear what the problem really is about.
Documentation has to be clear about the usage of functions, especially, when they accept different variations of parameters.
If both are not considered well enough, when designing a framework, developers (like op in this case) get stuck on problems that could have been easily avoided.

Using a specific seed in the `RANDOM_NUMBER` algorithm

I'm looking to use a specific set of seeds for the intrinsic function RANDOM_NUMBER (a PRNG). What I've read so far is that the seed value can be set via calling RANDOM_SEED, specifically RANDOM_SEED(GET = array). My confusion is how to (if it's possible) set a specific value for the algorithm, for instance in the RAND, RANDU, or RANDM algorithms one can specify their own seed directly. I'm not sure how to set the seed, as the get function seems to take an array. If it takes an array, does it always pull the seed value from a specific index in the array?
Basically, is there a way to set a specific single seed value? If so, would someone be able to write-it out?
As a side note - I'm attempting to set my seed because allegedly one of the other PRNGs I mentioned only works well with "large odd numbers" according to my professor, so I decided that I may as well control this when comparing the PRNG's.
First, the RANDOM_SEED is only used for controlimg the seed of RANDOM_NUMBER(). If you use any other random number generator subroutine or function, it will not affect them at all or if yes then in some compiler specific way which you must find in the manual. But most probably it does not affect them.
Second, you should not care at all whether the seed array contains 1, 4 or 42 integers, it doesn't matter because the PRNG uses the bits from the whole array in some unspecified custom way. For example you may be generating 64 bit real numbers, but the seed array is made of 32 bit integers. You cannot simply say which integer from the seed array does what. You can view the whole seed array as one big integer number that is cut into smaller pieces if you want.
Regarding your professors advice, who knows what he meant, but the seed is probably set by some procedure of that particular generator, and not by the standard RANDOM_SEED, so you must read the documentation for that generator.
And how to use a specific seed in RANDOM_SEED? It was described on this site several times,jkust search for RANDOM_SEED in the top right search field, really. But it is simple, once you know the size of the array, size it to any non-trivial numbers (you need enough non-zero bits) you want and use put=. That's really all, just don't think about individual values in the array, the whole array is one piece of data together.

Random number generation requires too many iterations

I am running simulations in Anylogic and I'm trying to calibrate the following distribution:
Jump = normal(coef1, coef2, -1, 1);
However, I keep getting the following message as soon as I start the calibration (experimentation):
Random number generation requires too many iterations (> 10000)
I tried to replace -1 and 1 by other values and keep getting the same thing.
I also tried to change the bounds of coef1 and coef2 and put things like [0,1], but I still get the same error.
I don't get it.
Any ideas?
The four parameter normal method is not deprecated and is not a "calibration where coef1 and coef2 are the coefficicents to be solved for". Where did you get that understanding from? Or are you saying that you're using your AnyLogic Experiment (possibly a multi-run or optimisation experiment) to 'calibrate' that distribution, in which case you need to explain what you mean by 'calibrate' here---what is your desired outcome?
If you look in the API reference (AnyLogic classes and functions --> API Reference --> com.xj.anylogic.engine --> Utilities), you'll see that it's a method to use a truncated normal distribution.
public double normal(double min,
double max,
double shift,
double stretch)
The first 2 parameters are the min and max (where it will sample repeatedly and ignore values outside the [min,max] range); the second two are effectively the mean and standard deviation. So you will get the error you mentioned if min or max means it will sample too many times to get a value in range.
API reference details below:
Generates a sample of truncated Normal distribution. Distribution
normal(1, 0) is stretched by stretch coefficient, then shifted to the
right by shift, after that it is truncated to fit in [min, max]
interval. Truncation is performed by discarding every sample outside
this interval and taking subsequent try. For more details see
normal(double, double)
Parameters:
min - the minimum value that this function will return. The distribution is truncated to return values above this. If the sample
(stretched and shifted) is below this value it will be discarded and
another sample will be drawn. Use -infinity for "No limit".
max - the maximum value that this function will return. The distribution is truncated to return values below this. If the sample
(stretched and shifted) is bigger than this value it will be discarded
and another sample will be drawn. Use +infinity for "No limit".
shift - the shift parameter that indicates how much the (stretched) distribution will shifted to the right = mean value
stretch - the stretch parameter that indicates how much the distribution will be stretched = standard deviation Returns:
the generated sample
According to AnyLogic's documentation, there is no version of normal which takes 4 arguments. Also note that if you specify a mean and standard deviation, the order is unusual (to probabilists/statisticians) by putting the standard deviation before the mean.

Multiply two numbers whose range is 10^18

There is a variable first_variable which is always a mod of some number, mod_value.
In every step first_variable is multiplied with some number second_variable.
And the range of all three variables is from 1 to 10^18.
For that I build a formula,
first_variable = ((first_variable%mod_value)*(second_variable%mod_value))%mod_value
But this gives a wrong answer,
For example, If first_variable and second_variable is (10^18)-1 and mod_value = 10^18
Please suggest me method, so that first_variable will always give right answer.
Seems you are using a runtime where arithmetic is implemented using 64-bit integers. You can check this using multipliers like 2^32: if their product is 0, my guess is true. In that case, you should switch to an arbitrary long arithmetic implementation, or at least one that is much longer than the current one. E.g. Python supports integers up to 2^1016 (256^127), same for Erlang.
I've seen in comments you use C++. If so, look for GMP library and analogs. Or, if 128 bits is enough, modern GCC support it through own library.
This is basically overflows, so you should either use different value for mod_value (up to 10^9) or limit the range for first value and second value.
Your number is O(10^36) which is O(2^108) which cannot fit in any primitive data type in languages like java or C++. Use BigInt in C++ or Java or use numpy in python to get over it.

Octave random Rational Numbers gernrator

I just start to study about Octave and I have a question about getting Rational Numbers.
I just check
http://www.gnu.org/software/octave/doc/interpreter/Random-Number-Generation.html#Random-Number-Generation
this page to learn the way to get random Rational Numbers.
for example..
if we use rand(1, 3.1)
i would like to get random number between 1 and 3.1 (like 2.34)
However, i am not really sure about function that i have to use..
can you give some example ?
thanks
The function unifrnd returns random numbers sampled from a uniform distribution. The first two arguments determine the lower and upper bounds. The remaining (optional) arguments determine the shape of the result. So, for example, to get random numbers between 1 and 3.1:
octave:12> unifrnd(1, 3.1)
ans = 2.4990
octave:13> unifrnd(1, 3.1)
ans = 3.0240
octave:14> unifrnd(1, 3.1, 2, 3)
ans =
1.8929 2.9675 2.1239
2.4756 2.6172 1.6197
(The results are regular floating point numbers. I don't understand why you are asking about rational numbers.)

Resources