Randomise List

Share your advanced PureBasic knowledge/code with the community.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Randomise List

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Randomise List

Post 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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Randomise List

Post 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
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Randomise List

Post 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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
BarryG
Addict
Addict
Posts: 3324
Joined: Thu Apr 18, 2019 8:17 am

Re: Randomise List

Post by BarryG »

collectordave wrote:Need to randomise a list.
Use the RandomizeList() command? Your code isn't using it?
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Randomise List

Post 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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Randomise List

Post 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.
Post Reply