Page 1 of 1

Randomise List

Posted: Tue Mar 02, 2021 8:33 am
by collectordave
Need to randomise a list.

Keeping the current element wherever it moves too.

Adapted the help code a bit and it seems to work.

Code: Select all

NewList Numbers()
  
  For k=0 To 10
    AddElement(Numbers())
    Numbers() = k
  Next
  
  SelectElement(Numbers(), 3)
  
  *CurrentElement = @Numbers()
  
  
  Debug ListIndex(Numbers())
  Debug Numbers()
  
  For iLoop = 0 To 10 ;10 is listsize(Numbers()) -1
  
  SelectElement(Numbers(), Random(10,0))
  *FirstElement = @Numbers()
  
  SelectElement(Numbers(), Random(10,0))
  *SecondElement = @Numbers()

  SwapElements(Numbers(), *FirstElement, *SecondElement)
  
  Next
  
  Debug ""
  
  iLoop = 0
  ForEach Numbers()
    
    
    If *CurrentElement = @Numbers()
      Break
    EndIf
    
    iLoop = iLoop + 1
  Next

  SelectElement(Numbers(),iLoop);?????
  Debug ListIndex(Numbers())
  Debug Numbers()
  
  Debug ""
  ForEach Numbers()
    Debug Numbers()
  Next
It works with structured lists as well.

Can anyone see a better way or any mistakes.

Re: Randomise List

Posted: Tue Mar 02, 2021 9:03 am
by collectordave
In my rush after this little success I tried it in my main code and found that if no element is selected it can fail.

Changed code to this to preserve the selected element.

Code: Select all

NewList Numbers()
  
  For k=0 To 10
    AddElement(Numbers())
    Numbers() = k
  Next
  
 ; ResetList(Numbers())
  SelectElement(Numbers(),7)
    
  SelectedElement = ListIndex(Numbers())
    
  If SelectedElement > -1 ;If An Element is selected
    SelectElement(Numbers(), SelectedElement)
    *CurrentElement = @Numbers()
  EndIf

  
  For iLoop = 0 To 10
  
  SelectElement(Numbers(), Random(10,0))
  *FirstElement = @Numbers()
  
  SelectElement(Numbers(), Random(10,0))
  *SecondElement = @Numbers()

  SwapElements(Numbers(), *FirstElement, *SecondElement)
  
  Next
  
  Debug ""

  
  If SelectedElement > -1
    iLoop = 0
    ForEach Numbers()
      If *CurrentElement = @Numbers()
        Break
      EndIf
      iLoop = iLoop + 1
    Next
  EndIf
  
  ;Test it iLoop = 0 when no element selected
  SelectElement(Numbers(),iLoop)
  Debug ListIndex(Numbers())
  Debug Numbers() ;Should be value of selected element
  
  Debug ""
  ForEach Numbers()
    Debug Numbers()
  Next
Hope I have it all now

CD

Re: Randomise List

Posted: Tue Mar 02, 2021 9:14 am
by Keya
heya Dave, Im not sure if this will be of any help but your post made me think of the LFSR PRNG, and I searched on PB but it hadnt been posted (I swear i'd posted it ages ago!) so I just posted it - viewtopic.php?f=12&t=76848
It has a unique property that might be helpful but im not sure

Re: Randomise List

Posted: Tue Mar 02, 2021 9:39 am
by collectordave
Hi Keya

Thanks for that I think it would be great for randomising an index to an array.

Unfortunately in my case I am using a list with an index of 0 plus I do not care how many times an element is moved in the list.

CD

Re: Randomise List

Posted: Tue Mar 02, 2021 9:46 am
by BarryG
collectordave wrote:Need to randomise a list.
Use the RandomizeList() command? Your code isn't using it?

Re: Randomise List

Posted: Wed Mar 03, 2021 7:00 am
by collectordave
Checked the help List Index and that one isn't listed!

Would be nice if it could be added in the docs.

Thanks for letting me know about it.

CD

Re: Randomise List

Posted: Wed Mar 03, 2021 7:45 am
by Shardik
collectordave wrote:Checked the help List Index and that one isn't listed!

Would be nice if it could be added in the docs.
The function RandomizeList() is described in the library Sort and not - as could be expexted - in the library List.