# How can I avoid pseudo random and confirm natural random ?

4 views (last 30 days)

Show older comments

In function rand and randi,maybe they are pseudorandom,I want to creat a natural random.

or can help me how to check function rand's isn't pseudorandom.(I have two data from,how can I check they)

##### 2 Comments

Steven Lord
on 17 Aug 2022

### Accepted Answer

John D'Errico
on 17 Aug 2022

Edited: John D'Errico
on 17 Aug 2022

It is not possible to do that. For example, suppose I give you a number, but I won't tell you where it came from, or how I generated it. Is it truly random, or did a random number generator generate the number? Even in a simple case of a coin flip. Suppose I give you the sequence [0 0 1]. Did I flip a coin? Did I use a random number generator? You cannot know, nor can any test tell you that.

There are many tests that can test the hypothesis that a sequence is not truly random. But you can never know for sure. You CANNOT confirm that something was randomly generated. Period. At best, you can decide that some sequence was unlikely to have been truly randomly generated.

An issue is that some sequence of random numbers fail due to a non-obvious flaw. For example, some of the old random number generators based on simple schemes would look very random, and would in fact pass some simple tests. But if you looked at the sequence generated in the right way, it was clear they were not truly random. The problem is, it is impossible to test for every possible such flaw. Again, KNOWING that a sequence is truly random is a problem. At best, you can only test to see if it is unlikely to be truly random, based on one of the many tests you can apply.

The old style, classic linear congruential random number generators were easy to write. We can make one up right now. For example, consider the function LCRNG. We can use it to generate pseudo-random numbers.

LCRNG(5)

LCRNG(5)

They look pretty random, even though I am sure the linear congruential random number generator I used is godawfully bad. (intentionally so.) The sequence you would generate would repeat, IF you asked for at least 33554467 elements in the vector, since it is carefully limited. So that is a theoretical problem, but only if you wanted to generate a LONG sequence of numbers. However, if I plot the sequence generated, it looks pretty random, no?

plot(LCRNG(10000),'.')

How about a histogram?

histogram(LCRNG(1000000),1000)

To be honest, it still looks pretty good, quite uniform. No?

The problem is, this particular class of random sequence has a fatal flaw, one that you might not see until you think about how the numbers weree generated.

X = LCRNG(10000);

X = reshape(X,2,[]);

plot(X(1,:),X(2,:),'.')

WHOOOPS! Now we see the flaw in this class of random number generator. I intentionally built something that had a fatal flaw, but only if you look at it the right way.

function X = LCRNG(N)

% A POOR linear congruential random number generator

% n = the number of elements to be generated as a ROW vector of length n

persistent seed

L = 33554467;

a = 3;

c = 25165981;

if isempty(seed)

seed = fix(max(1,mod(now,1)*(L-1) ));

end

X = zeros(1,N);

for ind = 1:N

seed = mod(seed*a + c,L);

X(ind) = seed/L;

end

end

And that is a fundamental problem. Even if you are given a sequence of numbers, you may need to know how they may fail to be random. What test might you apply?

In the end, I'll go back to my original statement. You cannot know for sure that a sequence is truly random, or just something that looks pretty darn good, and passes every test you can think of. And even if you apply some test to a generated sequence, you can only assert a probabilistic assessment of the test. For example, the sequence [1 1 1 1 1 1 1 1] may arise from a sequence of coin flips. It is equally as likely as the specific sequence [1 1 0 1 1 0 0 1].

##### 0 Comments

### More Answers (2)

Walter Roberson
on 17 Aug 2022

You cannot write a deterministic program that gives a truly random output. You need to measure some external event, and then you need to de-correlate the measurements against each other.

For example you could read the Intel cycle counters, technically known as Timer Stamp Counter; https://www.mathworks.com/matlabcentral/answers/99980-is-it-possible-to-analyze-exactly-how-long-a-process-takes-to-execute-so-that-cpu-overload-can-be#answer_109328 . However, those will tend to be correlated. not unbiased.

There are web sites that you can connect to using tcpclient() in order to request some random numbers.

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!