Generate pseudo random numbers problem [ SOLVED ]

3 vues (au cours des 30 derniers jours)
Antonios
Antonios le 28 Mar 2015
Modifié(e) : Antonios le 29 Mar 2015
function [ Random_Numbers ] = RNG_2( n, seed )
format long;
seed_square = seed;
Random_Numbers = zeros(1,0); %Random_Numbers vector
for counter = 1:127 %calculations
Random_Numbers(end+1) = seed_square^2 / (10^(2*n));
seed_square = num2str(seed_square^2,9);
seed_square = seed_square(2:3);
seed_square = str2num(seed_square);
end
format;
Random_Numbers=sort(Random_Numbers); %sort numbers
plot(Random_Numbers) %plot
end
I have this code and i want to generate 127 random numbers. The problem is that matlab gives numbers at this forma 0.XXX. Besides, i convert it to str and i get the 2 middle digits for next number calculation. Sometimes number is at this format 0.00XX or 0.0XX. num2str doesnt give 00XX or 0XX and i have either wrong calculations or error concerning the dimensions. Any ideas ? Thank you very much for your time.

Réponses (3)

Jan
Jan le 28 Mar 2015
format long modifies the output to the command window - are you sure this is useful here?
You forgot to explain the value of the input seed. Is this a vector? What are "the middle digits"? The middle of what? What about a direct sprintf('%.9f') instead of the smart high-level function num2str.
The processing is much faster if the variables do not change their type. rem(see_square * 10000, 100) would be more direct, simple and efficient.

Antonios
Antonios le 28 Mar 2015
You are right. I had to be specific. n = 2, seed = 85. Everytime seed goes seed^2. The first time seed^2 = 7225. I need the n middle digits. This is my problem.

Antonios
Antonios le 29 Mar 2015
[SOLVED] The operator ^ made the problem. I used the function power and everything worked immediately !

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by