# How can I pseudorandomize with constrains and make spesific number spread equally among the array?

4 views (last 30 days)
Doli Swey on 13 Sep 2020
Commented: Doli Swey on 16 Sep 2020
Hello everybody,
I have a question regarding to pseudorandomization and more. Sorry I am new on Matlab and I feel very stuck!
I have an array consist of 4 numbers (1,2,3,4). The lenght of the array is 330. Number 1,2,3 are repeated 100 times, whereas number 4 is repeated 30 (%10 of total)times.
A=[1,1,2,3,4,2,3.....]
How can I randomize this array in a way that the maximum number of repetition of spesific number is 3 (for expample [1,3,2,2,2,2,1,1,1,3,..] is not correct) and
the number 4 is positioned more less equily spreed along the array (like in every 8-12 position)?
Thank you very much.
Stay happy and healthy!

Bruno Luong on 14 Sep 2020
Unzip this attached file, you'll get a pfile r1234.p
Call it
A = r1234
You'll get the result as specified.
Doli Swey on 16 Sep 2020
Thank you so much Bruno.

Walter Roberson on 13 Sep 2020
the number 4 is positioned more less equily spreed along the array (like in every 8-10 position)?
You cannot achieve that constraint.
If you were to distribute the 30 entries that are 4 equally across the 330 positions, they would have to be every 11 apart. If you put them closer together (maximum 10 apart) then that can only occupy at most 300 of the 330 entries, leaving an uncovered gap of at least 15 on both sides. If you say that the minimum distance must be 8 and the maximum must be 10, then you cannot spread the 30 entries out over the 330 elements of the array without ending up with a portion of the array for which that does not hold.
Doli Swey on 15 Sep 2020
Great Thank you Walter. I have learn a lot by your coding mentality. All the best!