choose()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
tikidays
User
User
Posts: 46
Joined: Tue Oct 24, 2023 6:47 am
Location: New Zealand
Contact:

choose()

Post by tikidays »

I would like to see a new function called choose() that randomly selects data from a supplied series of items. Used this a lot with past environments.
Examples:

choose("Hello", "World","To", "The")

returns: "World"

choose( 1,3,7,8)
returns 3

Code: Select all

name.s = choose("Tod", "Paula","Fred","Bill")
debug name
Bill
Quin
Addict
Addict
Posts: 1131
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: choose()

Post by Quin »

+1. This is one of those incredibly annoying things to implement from the user side because we don't have varargs.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: choose()

Post by Little John »

Code: Select all

Procedure.s RandomChoose (myList$, sep$=",")
   Protected.i last, item
   
   last = CountString(myList$, sep$)
   item = Random(last) + 1
   
   ProcedureReturn StringField(myList$, item, sep$)
EndProcedure


Debug RandomChoose("Hello,World,To,The")
Post Reply