如何在MATLAB中不使用rand函数生成类似b=round(rand(1,20))的随机序列?
Hey there! If you need to generate random-like number sequences in MATLAB without using the built-in rand function, here are a few solid approaches you can try out—each with its own use case:
This is the simplest method, leveraging the microsecond or millisecond portion of the current system time to generate non-repeating (for short intervals) values. Perfect for quick tests where you don't need industrial-grade randomness.
function random_seq = time_based_rand(num_elements) % Grab the current time with microsecond precision current_time = datetime('now', 'Format', 'yyyy-MM-dd HH:mm:ss.SSSSSS'); microseconds = str2double(split(char(current_time), '.'){2}); % Generate a sequence by offsetting the seed and taking modulo % Adjust the modulo value to match your desired range (here 0 or 1, like round(rand)) random_seq = mod(microseconds + (1:num_elements)*13, 2); end % Generate your 1x20 sequence b = time_based_rand(20);
Note: This will repeat if you call it within the same microsecond, so it's not ideal for high-volume or rapid calls.
LCGs are the backbone of many built-in random number generators. They use a mathematical formula to produce a sequence with a long period (if you pick the right parameters). This is way more reliable than the time-based method for most applications.
function random_seq = lcg_generator(num_elements, min_val, max_val) % Proven LCG parameters (from Numerical Recipes) a = 1664525; c = 1013904223; m = 2^32; % Use system time to initialize the seed seed = floor(datetime('now', 'ConvertFrom', 'datenum') * 86400000); random_seq = zeros(1, num_elements); for i = 1:num_elements seed = mod(a * seed + c, m); % Normalize to 0-1, map to your desired range, then round random_seq(i) = round((seed / m) * (max_val - min_val) + min_val); end end % Generate a 1x20 sequence matching round(rand(1,20)) (0 or 1) b = lcg_generator(20, 0, 1); % If you wanted integers from 0 to 20 instead, just call: % b = lcg_generator(20, 0, 20);
Pro Tip: The parameters a, c, and m are chosen to maximize the period of the sequence—stick with tried-and-true values unless you know what you're doing.
If you need sequences with strong statistical randomness, chaotic maps like the Logistic map are a great choice. When tuned to the right parameter, they produce non-repeating, unpredictable sequences.
function random_seq = logistic_chaos_rand(num_elements) % Chaos parameter (this value ensures full chaotic behavior) r = 3.5699456; % Initialize with a value derived from system time (no rand functions!) time_seed = mod(datetime('now', 'datenum') * 1e6, 1); x = time_seed; random_seq = zeros(1, num_elements); for i = 1:num_elements x = r * x * (1 - x); % Map the chaotic value to 0 or 1 (matching round(rand)) random_seq(i) = round(x); end end % Generate your 1x20 sequence b = logistic_chaos_rand(20);
Heads Up: Chaotic sequences are super sensitive to initial conditions—even tiny changes in the seed will produce completely different results.
内容的提问来源于stack exchange,提问作者Ketaki_S

