Filling an array with random numbers in C

Studying arrays, I came to the question, how can I create an array and fill it with absolutely random values? There are resources on the Internet that provide code for solving this problem, but unfortunately without explanations, and I would like to understand what functions are responsible for this, how many different methods there are, and what ways it is best to do it so that it really is random.

Author: Harry, 2019-01-20

1 answers

If C++ - then there is now a reliable and efficient library included in the standard <random>. If you use C, then you need to use only the built-in generator rand() or write your own, relying, say, on Knut. Of course, these are not absolutely random numbers, but pseudo-random ones.

Your question is not very precise - for example, what distribution of numbers in the matrix should there be? Uniform, normal, or what else?

This usually looks like

int r[N];
for(int i = 0; i < N; ++i) r[i] = rand();

Well, or, say, for random numbers in the range from A to B (only B-A+1 should not exceed RAND_MAX) -

r[i] = rand()%(B-A+1) + A;

If you specify your question , you can specify the answer :)

 2
Author: Harry, 2019-01-20 12:00:19