How can I avoid pseudo random and confirm natural random ?

1 次查看(过去 30 天)
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)

采纳的回答

John D'Errico
John D'Errico 2022-8-17
编辑:John D'Errico 2022-8-17
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)
ans = 1×5
0.1331 0.1493 0.1979 0.3438 0.7815
LCRNG(5)
ans = 1×5
0.0944 0.0332 0.8496 0.2988 0.6464
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].

更多回答(2 个)

Walter Roberson
Walter Roberson 2022-8-17
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.

Jan
Jan 2022-8-17
编辑:Jan 2022-8-17

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by