A bunch of values has to be spread into an array randomly. None may be double. Random values are allowed and do not have to be in ascending or descending order.


It works a bit like playing roulette with many bowls. The values, pushed on the parameter buffer, will be put into the array by looping through the index of the array. A random index chooses the next lot. If it is occupied (<>0) the index jumps 1 lot further, until it finds one free.


DIM array[4] : n=4 !»» destination array
PUT 4,12,7,16
!»» values to spread

FOR i=1 TO n !»» loop to get all values
  s=2+INT(RND(n)) : j=0
  WHILE s>0 DO !»» loop to find a free position
    j=j+1 : IF j>n THEN j=1
!»» loop through the array positions
    IF array[j]=0 THEN s=s-1 !»» jump over occupied positions
  NEXT i