Generate pseudo random numbers problem [ SOLVED ]

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.

Respuestas (3)

Jan
Jan el 28 de Mzo. de 2015

0 votos

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 el 28 de Mzo. de 2015

0 votos

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 el 29 de Mzo. de 2015

0 votos

[SOLVED] The operator ^ made the problem. I used the function power and everything worked immediately !

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Mzo. de 2015

Editada:

el 29 de Mzo. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by