Good code - Arrays & Random Selections

antonylord

New Member
Coding challenge :

Pick a random number between 0-7
Now randomly pick one of the remaining 6 numbers
Now randomly pick one of the remaining 5 numbers
etc.

eg 1,4,6,7,2,0,3,5 (each number is used only once)

Now I remember years ago doing this in C with a linked array and each time you chose a number your shortened the array by changing the pointers. This was elegant as the list from which you could randomly choose the next number only contains the previously unchosen numbers.

How to do this in pbasic?

I can write an awful bt of code using a 1 x 8 array where each time I "use" a number I flag it then keep randomly choosing until I get an unused number but hey - that's not efficient at all.

Anyone shed some light on a smart solution?

Cheers, Antony.
 
Last edited:

bgrabowski

Senior Member
Store the numbers 0-7 in 8 bytes of RAM. Shuffle the numbers using 2 randomly generated pointers to swap a pair of the numbers, repeating this say 100 times. The 8 numbers will then be randomly arranged. Accessing these numbers from RAM, in order, will give a jumbled set of 0-7 with no repeats.
 

hippy

Ex-Staff (retired)
You should be able to get away with repeatedly swapping the first number with a ( random decided ) Nth number which simplifies bgrabowski's idea further.
 

lbenson

Senior Member
If OP did this years before in C, this doesn't sound like homework. Uses lots of variables and requires 28/40X1 for timer, but try this in the simulator.

Code:
#picaxe 28x1

eeprom 0,(0,1,2,3,4,5,6,7)

w6 = timer
for b9 = 0 to 7
  random w6
  b8 = 8 - b9
  b11 = w6 // b8
  read b11,b10
  sertxd (b10)
  dec b8
  if b11 <> b8 then ' need to swap
    read b8,b7      ' take highest remaining value
    write b11,b7    ' put in slot for used value
  endif
next b9
"the list from which you could randomly choose the next number only contain the previously unchosen numbers"
 
Last edited:

moxhamj

New Member
eg 1,4,6,7,2,0,3,5 (each number is used only once)

So you are only outputting numbers and the final output of the program is a series of 8 numbers 0 to 7 in random order? If so, the internals of the program are not necessary to know so you could certainly pick one number, then shorten the list by 1 then pick another number.

But the output would be the same if you had a list of numbers

0,1,2,3,4,5,6,7

Put them in an array (or in picaxe, poke them into some ram locations).

Pick a random number 0 to 7 = a
Pick another random number 0 to 7 = b
Swap the numbers in ram location a with ram location b.
Do this a random number of times.

I think this is the same as bgrabowski and hippy's solution.

Does the picaxe need randomizing to prevent the same sequence coming up - eg a white noise source (? a transistor) and read the value with readadc, or detecting the delay between two keypresses?
 
Last edited:

cvrwy

Member
I would substitute the used NUMBER with another character, a LETTER, for instance, and continue processing....... Unless maybe that's against the rules.
 
Top