Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Possible Issue with Jan Simon's "Shuffle" Program

Subject: Possible Issue with Jan Simon's "Shuffle" Program

From: Ulrik Nash

Date: 13 Jul, 2010 10:31:05

Message: 1 of 4

Hi Everyone,

I have applied Jan Simon's "Shuffle" program in a simulation I am working on right now. Part of the simulation is a loop n times, where at each loop a random order of x numbers is supposed to be generated. But, I have noticed something, which I wonder if others, ideally Jan, can clear up:

For a given x numbers to be shuffled, the order of the numbers that Shuffle produces is the same across simulations. So, lets say I run 5 simulations of the loop. For each of the five simulations, the order of the x numbers produced at a given loop number is the same. Each loop is not the same, but the first loop of the first run simulation is the same as the first loop of the second, the third, the fourth, and so on.

If I select another value of x, the same occurs, but with a different order of numbers.

In other words, across multiple simulations, Shuffle is not random.

I bet am probably doing something wrong, but in any case I would like to hear some comments on this.

Kind regards,

Ulrik.

Subject: Possible Issue with Jan Simon's "Shuffle" Program

From: Ulrik Nash

Date: 13 Jul, 2010 11:31:04

Message: 2 of 4

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i1hf99$kb5$1@fred.mathworks.com>...
> Hi Everyone,
>
> I have applied Jan Simon's "Shuffle" program in a simulation I am working on right now. Part of the simulation is a loop n times, where at each loop a random order of x numbers is supposed to be generated. But, I have noticed something, which I wonder if others, ideally Jan, can clear up:
>
> For a given x numbers to be shuffled, the order of the numbers that Shuffle produces is the same across simulations. So, lets say I run 5 simulations of the loop. For each of the five simulations, the order of the x numbers produced at a given loop number is the same. Each loop is not the same, but the first loop of the first run simulation is the same as the first loop of the second, the third, the fourth, and so on.
>
> If I select another value of x, the same occurs, but with a different order of numbers.
>
> In other words, across multiple simulations, Shuffle is not random.
>
> I bet am probably doing something wrong, but in any case I would like to hear some comments on this.
>
> Kind regards,
>
> Ulrik.



I have found a solution, and the issue was human error on my part, being quite new to Matlab.

My loop was (basically) as follows:

for n:10
B = Shuffle(A)
end

I have found that I need to do the following to ensure an update the shuffle each loop.:

for n:10
Shuffle(A);
B = Shuffle(A);
end

Regards,

Ulrik.

Subject: Possible Issue with Jan Simon's "Shuffle" Program

From: Jan Simon

Date: 14 Jul, 2010 13:40:09

Message: 3 of 4

Dear Ulrik,

> > For a given x numbers to be shuffled, the order of the numbers that Shuffle produces is the same across simulations. So, lets say I run 5 simulations of the loop. For each of the five simulations, the order of the x numbers produced at a given loop number is the same. Each loop is not the same, but the first loop of the first run simulation is the same as the first loop of the second, the third, the fourth, and so on.
> >
> > If I select another value of x, the same occurs, but with a different order of numbers.
> > In other words, across multiple simulations, Shuffle is not random.

> I have found a solution, and the issue was human error on my part, being quite new to Matlab.
>
> My loop was (basically) as follows:
> for n:10
> B = Shuffle(A)
> end
>
> I have found that I need to do the following to ensure an update the shuffle each loop.:
> for n:10
> Shuffle(A);
> B = Shuffle(A);
> end

Your soultion should definitely not work. "Shuffle(A)" does change the internal random number generator, but in a determinable way.
I assume, that you initialize Shuffle with a certain random seed in each of your simulations. This happens either with:
  Shuffle(13286976, 'seed');
or by clearing Shuffle from the workspace - then a standard seed is used by default:
  clear('Shuffle')
or
  clear all

Try it:
  clear all
  x1 = Shuffle(1:10)
  clear all
  x2 = Shuffle(1:10)
The same sequence is replied!
  clear all
  Shuffle(1:10);
  x1 = Shuffle(1:10)
  clear all
  Shuffle(1:10);
  x2 = Shuffle(1:10)
Again x1 and x2 are equal.

A "simulation" should be reproducible. Therefore it must be possible to start the simulation with the same RNG seed and this seed should be saved ith the results of the simulation.

Solutions:
1. Clearing all variables is crude, so omit it.
2. Initialize Shuffle's RNG with an apropriate seed if you use it for real simulations. This is scientifically necessary and important. The best would be to get 4 random 32 bit itegers from www.random.org. 2 tools have been published in the FEX in the last month.
3. I could insert a mexLock() command, when the standard seed is used. This would save Shuffle from bein cleared from the workspace.

Kind regards, Jan

Subject: Possible Issue with Jan Simon's "Shuffle" Program

From: Jan Simon

Date: 14 Jul, 2010 23:05:08

Message: 4 of 4

Dear Ulrik,

I'll insert the mexLock, because the behaviour is equivalent to RAND then.

During some tests concerning your problem, I found a bug for seeds in [1 x 4] DOUBLE format - I'll fix it although obviously nobody needed this feature...

Kind regards, Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us