Page 1 of 1

choose()

Posted: Sun Jan 14, 2024 7:39 pm
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

Re: choose()

Posted: Sun Jan 14, 2024 10:55 pm
by Quin
+1. This is one of those incredibly annoying things to implement from the user side because we don't have varargs.

Re: choose()

Posted: Mon Jan 15, 2024 12:02 am
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")