Generate pseudo random numbers problem [ SOLVED ]
Show older comments
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.
Answers (3)
Jan
on 28 Mar 2015
0 votes
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
on 28 Mar 2015
0 votes
Antonios
on 29 Mar 2015
0 votes
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!