Pick random element: Difference between revisions
Content deleted Content added
→{{header|PicoLisp}}: Added PureBasic |
|||
Line 225: | Line 225: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
<lang PicoLisp>(get Lst (rand 1 (length Lst)))</lang> |
<lang PicoLisp>(get Lst (rand 1 (length Lst)))</lang> |
||
=={{header|PureBasic}}== |
|||
<lang PureBasic>Procedure.s pickRandomElement(List source.s()) |
|||
Protected x = ListSize(source()) |
|||
If x > 0 |
|||
SelectElement(source(), Random(x - 1)) ;element numbering is zero - based |
|||
ProcedureReturn source() |
|||
EndIf |
|||
EndProcedure |
|||
;initialize list elements |
|||
DataSection |
|||
elements: |
|||
Data.s "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" |
|||
EndDataSection |
|||
#elementCount = 10 |
|||
NewList item.s() |
|||
Restore elements |
|||
Define i |
|||
For i = 1 To #elementCount |
|||
AddElement(item()) |
|||
Read.s item() |
|||
Next |
|||
If OpenConsole() |
|||
Print("Source list: ") |
|||
ForEach item() |
|||
Print(item() + " ") |
|||
Next |
|||
PrintN(#CRLF$) |
|||
Print("Random picks from list: ") |
|||
For i = 1 To 10 |
|||
Print(pickRandomElement(item()) + " ") |
|||
Next |
|||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
|||
CloseConsole() |
|||
EndIf</lang> |
|||
Sample output: |
|||
<pre>Source list: One Two Three Four Five Six Seven Eight Nine Ten |
|||
Random picks from list: Seven Nine Two Six Four Four Nine Three Six Two</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |