What Difference Between Rand() and Random() Functions

What difference between rand() and random() functions?

randomize() and random() are not part of the standard library. Perhaps your teacher wrote functions with these names for use in your class, or maybe you really mean random() and srandom() which are part of POSIX and not available on Windows. rand() and srand() are part of the standard library and will be provided by any standard conforming implementation of C++.


You should avoid rand() and srand() and use the new C++11 <random> library. <random> was added as part of the C++11 standard (and VS2012 does provide it).

Video explaining why: rand() Considered Harmful

  • rand() is sometimes a low quality pRNG and not suitable for applications that need a reasonable level of unpredictability. <random> provides a variety of engines with different characteristics suitable for many different use cases.

  • Converting the results of rand() into a number you can use directly usually relies on code that is difficult to read and easy to get wrong, whereas using <random> distributions is easy and produces readable code.

  • The common methods of generating values in a given distribution using rand() further decrease the quality of the generated data. % generally biases the data and floating point division still produces non-uniform distributions. <random> distributions are higher quality as well as more readable.

  • rand() relies on a hidden global resource. Among other issues this causes rand() to not be thread safe. Some implementations make thread safety guarantees, but this is not required by the standard. Engines provided by <random> encapsulate pRNG state as objects with value semantics, allowing flexible control over the state.

  • srand() only permits a limited range of seeds. Engines in <random> can be initialized using seed sequences which permit the maximum possible seed data. seed_seq also implements a common pRNG warm-up.


example of using <random>:

#include <iostream>
#include <random>

int main() {
// create source of randomness, and initialize it with non-deterministic seed
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
std::mt19937 eng{seed};

// a distribution that takes randomness and produces values in specified range
std::uniform_int_distribution<> dist(1,6);

for (int i=0; i<100; ++i) {
std::cout << dist(eng) << '\n';
}
}

What difference between rand() and random() functions?

randomize() and random() are not part of the standard library. Perhaps your teacher wrote functions with these names for use in your class, or maybe you really mean random() and srandom() which are part of POSIX and not available on Windows. rand() and srand() are part of the standard library and will be provided by any standard conforming implementation of C++.


You should avoid rand() and srand() and use the new C++11 <random> library. <random> was added as part of the C++11 standard (and VS2012 does provide it).

Video explaining why: rand() Considered Harmful

  • rand() is sometimes a low quality pRNG and not suitable for applications that need a reasonable level of unpredictability. <random> provides a variety of engines with different characteristics suitable for many different use cases.

  • Converting the results of rand() into a number you can use directly usually relies on code that is difficult to read and easy to get wrong, whereas using <random> distributions is easy and produces readable code.

  • The common methods of generating values in a given distribution using rand() further decrease the quality of the generated data. % generally biases the data and floating point division still produces non-uniform distributions. <random> distributions are higher quality as well as more readable.

  • rand() relies on a hidden global resource. Among other issues this causes rand() to not be thread safe. Some implementations make thread safety guarantees, but this is not required by the standard. Engines provided by <random> encapsulate pRNG state as objects with value semantics, allowing flexible control over the state.

  • srand() only permits a limited range of seeds. Engines in <random> can be initialized using seed sequences which permit the maximum possible seed data. seed_seq also implements a common pRNG warm-up.


example of using <random>:

#include <iostream>
#include <random>

int main() {
// create source of randomness, and initialize it with non-deterministic seed
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
std::mt19937 eng{seed};

// a distribution that takes randomness and produces values in specified range
std::uniform_int_distribution<> dist(1,6);

for (int i=0; i<100; ++i) {
std::cout << dist(eng) << '\n';
}
}

Differences between numpy.random.rand vs numpy.random.randn in Python

First, as you see from the documentation numpy.random.randn generates samples from the normal distribution, while numpy.random.rand from a uniform distribution (in the range [0,1)).

Second, why did the uniform distribution not work? The main reason is the activation function, especially in your case where you use the sigmoid function. The plot of the sigmoid looks like the following:

Sample Image

So you can see that if your input is away from 0, the slope of the function decreases quite fast and as a result you get a tiny gradient and tiny weight update. And if you have many layers - those gradients get multiplied many times in the back pass, so even "proper" gradients after multiplications become small and stop making any influence. So if you have a lot of weights which bring your input to those regions you network is hardly trainable. That's why it is a usual practice to initialize network variables around zero value. This is done to ensure that you get reasonable gradients (close to 1) to train your net.

However, uniform distribution is not something completely undesirable, you just need to make the range smaller and closer to zero. As one of good practices is using Xavier initialization. In this approach you can initialize your weights with:

  1. Normal distribution. Where mean is 0 and var = sqrt(2. / (in + out)), where in - is the number of inputs to the neurons and out - number of outputs.

  2. Uniform distribution in range [-sqrt(6. / (in + out)), +sqrt(6. / (in + out))]

Difference between mt_rand() and rand()

Update

Since PHP 7.1 mt_rand has superseded rand completely, and rand was made an alias for mt_rand. The answer below focuses on the differences between the two functions for older versions, and the reasons for introducing mt_rand.



Speed was not why mt_rand was introduced!

The rand function existed way before mt_rand, but it was deeply flawed. A PRNG must get some entropy, a number from which it generates a sequence of random numbers. If you print out a list of ten numbers that were generated by rand() like so:

for ($i=0;$i<10;++$i)
echo rand(), PHP_EOL;

The output can be used to work out what the rand seed was, and with it, you can predict the next random numbers. There are tools out there that do this, so google a bit and test it.

There's also an issue with rand relativily quickly showing patterns in its random numbers as demonstrated here. A problem mt_rand seems to solve a lot better, too.

mt_rand uses a better randomization algorithm (Mersenne Twist), which requires more random numbers to be known before the seed can be determined and is faster. This does not mean that mt_rand is, by definition, faster than rand is, this only means that the way the numbers are generated is faster, and appears to have no real impact on the function's performance, as other answers here have demonstrated.

Either way, have a look at the mt_srand and the srand docs. I'm sure they'll contain some more info

If mt_rand's algorithm translates in an increase in performance, then that's great for you, but it's a happy coincidence. TL;TR:

mt_rand was introduced to fix the problems that exist in rand!

Difference between various numpy random functions

Nothing.

They're just aliases to random_sample:

In [660]: np.random.random
Out[660]: <function random_sample>

In [661]: np.random.ranf
Out[661]: <function random_sample>

In [662]: np.random.sample
Out[662]: <function random_sample>

In [663]: np.random.random_sample is np.random.random
Out[663]: True

In [664]: np.random.random_sample is np.random.ranf
Out[664]: True

In [665]: np.random.random_sample is np.random.sample
Out[665]: True

`numpy.empty()` and `numpy.random.rand()` same or different

They are different!

Rand, based on a seed generate some numbers of a given shape.

On the other part, empty return an unitializated array, so it means that is pointing to a random memory location, accidentally this return random values, but out of control, in your example gc has released the memoty location so the same area was occupied by empty with the same shape.

So don't use empty for generate random values

np.random.rand vs np.random.random

First note that numpy.random.random is actually an alias for numpy.random.random_sample. I'll use the latter in the following. (See this question and answer for more aliases.)

Both functions generate samples from the uniform distribution on [0, 1). The only difference is in how the arguments are handled. With numpy.random.rand, the length of each dimension of the output array is a separate argument. With numpy.random.random_sample, the shape argument is a single tuple.

For example, to create an array of samples with shape (3, 5), you can write

sample = np.random.rand(3, 5)

or

sample = np.random.random_sample((3, 5))

(Really, that's it.)


Update

As of version 1.17, NumPy has a new random API. The recommended method for generating samples from the uniform distribution on [0, 1) is:

>>> rng = np.random.default_rng()  # Create a default Generator.
>>> rng.random(size=10) # Generate 10 samples.
array([0.00416913, 0.31533329, 0.19057857, 0.48732511, 0.40638395,
0.32165646, 0.02597142, 0.19788567, 0.08142055, 0.15755424])

The new Generator class does not have the rand() or random_sample() methods. There is a uniform() method that allows you to specify the lower and upper bounds of the distribution. E.g.

>>> rng.uniform(1, 2, size=10)
array([1.75573298, 1.79862591, 1.53700962, 1.29183769, 1.16439681,
1.64413869, 1.7675135 , 1.02121057, 1.37345967, 1.73589452])

The old functions in the numpy.random namespace will continue to work, but they are considered "frozen", with no ongoing development. If you are writing new code, and you don't have to support pre-1.17 versions of numpy, it is recommended that you use the new random API.

Does rand() function with the same seed gives the same random numbers on different PC's?

No. rand implementation is not standartized and different compiler vedors can and will use different algorithms.

You can use generators from C++11 <random> header which are standard and completely determenistic: mt19937 with same seed should give same sequence on all platforms, for example.

When to use numpy.random.randn(...) and when numpy.random.rand(...)?

The difference between rand and randn is (besides the letter n) that rand returns random numbers sampled from a uniform distribution over the interval [0,1), while randn instead samples from a normal (a.k.a. Gaussian) distribution with a mean of 0 and a variance of 1.

In other words, the distribution of the random numbers produced by rand looks like this:

Uniform distribution

In a uniform distribution, all the random values are restricted to a specific interval, and are evenly distributed over that interval. If you generate, say, 10000 random numbers with rand, you'll find that about 1000 of them will be between 0 and 0.1, around 1000 will be between 0.1 and 0.2, around 1000 will be between 0.2 and 0.3, and so on. And all of them will be between 0 and 1 — you won't ever get any outside that range.

Meanwhile, the distribution for randn looks like this:

Normal distribution

The first obvious difference between the uniform and the normal distributions is that the normal distribution has no upper or lower limits — if you generate enough random numbers with randn, you'll eventually get one that's as big or as small as you like (well, subject to the limitations of the floating point format used to store the numbers, anyway). But most of the numbers you'll get will still be fairly close to zero, because the normal distribution is not flat: the output of randn is a lot more likely to fall between, say, 0 and 0.1 than between 0.9 and 1, whereas for rand both of these are equally likely. In fact, as the picture shows, about 68% of all randn outputs fall between -1 and +1, while 95% fall between -2 and +2, and about 99.7% fall between -3 and +3.

These are completely different probability distributions. If you switch one for the other, things are almost certainly going to break. If the code doesn't simply crash, you're almost certainly going to get incorrect and/or nonsensical results.



Related Topics



Leave a reply



Submit