How to make the range shuffle a random amount of times.

Hello all,
I need my range to shuffle a random ammout of times. So, what I intend to do is generate a randi(10000000000000) and then set that equal to s and then do the rng shuffle command. I just want it to shuffle that s ammount of times. That way it makes it different every time I open matlab it is randomized hopefully differently.
Thanks!

 Accepted Answer

"That way it makes it different every time I open matlab it is randomized hopefully differently."
Option 1: shuffle
rng('shuffle')
This command will choose a random seed based on the current time (sub-second resolution).
Option 2: randi
Another way to go about this is to save the seed to file and draw again if the seed has been previously chosen. Also, the seed can be any positive integer up to (2^32 - 1) so don't limit yourself with the number you arbitrarily chose.
This example assumes you have a mat file named 'seedlist.mat' that stores a vector named 'previousSeeds'.
%retrieve previous seeds
seedFile = load('seedlist.mat', 'previousSeeds');
previousSeeds = seedFile.previousSeeds;
% set rng, check for uniqueness, redo if needed
isUnique = false;
while ~isUnique
scurr = rng(randi(2^32-1)); %save seed & state; or scurr=rng('shuffle')
if ~ismember(previousSeeds, scurr.Seed)
isUnique = true;
end
end
% Store new seed to list on file
previousSeeds(end+1) = scurr.Seed;
save('seedlist.mat', 'previousSeeds', '-append')
I would also recommend you store a second variable that lists the date/time stamp that each seed was set. So you'd have a second vector that is the same size as previousSeeds. That way you have a log of what seeds were used so you can reproduce your data when needed.
One last word of caution, this affects everything that uses a random process in matlab so only do this when necessary.

4 Comments

Thanks! Very interesting. Is there a way to actauly intigrate that into my code? I'm making a music shuffler and I don't want it to be the same every time I open matlab. So, what I would like to do is a range change inside the program. Is this possible?
Ah, I see. Given that context, I want to make a completely different recommendation.
If your program depends on randomizing the seed, just change the seed using 'shuffle' when you open your music program and don't worry about accidentally replicating the same seed between matlab sessions. The chances of replicating the same seed from 2^32 seeds is infinitesimal. You could open your program 100 times a day for years before you duplicate a seed and your human memory capacity isn't nearly large enough to even recognize when this super rare event would happen.
rng('shuffle')
rng('shuffle') uses the time to generate a random seed.
If you had just opened MATLAB, then that is equivalent to rng(0,'twister'), and randi(2^32-1) is always going to be the same in that circumstance.
So if I use the rng(shuffle) command, then it will convieveably be differnt unless its the same time of day as the last time I opened matlab? Very cool!

Sign in to comment.

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Products

Release

R2018b

Asked:

on 1 Apr 2019

Edited:

on 2 Apr 2019

Community Treasure Hunt

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

Start Hunting!