Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Mastering Change Data Capture: Read Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Pick() random order

Hi,

I need to make a Pick() statement, where the order of the expressions is random every time you reload.

Example:


Load

RecNo() as DoorNo,

Pick(RecNo(),'Car','Goat','Goat') as DoorPrize,

AutoGenerate 3,

DoorNo & '|' & DoorPrize as Door;

should sometimes result in 1|Car 2|Goat 3|Goat, sometimes in 1|Goat 2|Car 3|Goat, and sometimes in 1|Goat 2|Goat 3|Car

Thanks for your help in advance,

Lucas

My actual script:

Load *,

     DoorNo & '|' & DoorPrize as Door;

Load

     RecNo() as DoorNo,

     Pick(RecNo(),'Car','Goat','Goat') as DoorPrize

     AutoGenerate 3;

14 Replies
rbecher
MVP
MVP

Hi Lucas,

probably this works (if I understand it right):

StartValue:

Load

     Ceil(Rand()*3) as Random

AutoGenerate 1;

Shuffle:

Load *,

     DoorNo & '|' & DoorPrize as Door;

Load

     RecNo() as DoorNo,

     Pick(Mod(Peek('Random', 0, 'StartValue')+RecNo(),3)+1,'Car','Goat','Goat') as DoorPrize

AutoGenerate 3;

- Ralf

Data & AI Engineer at Orionbelt.ai - a GenAI Semantic Layer Venture, Inventor of Astrato Engine
Not applicable
Author

Hi,

This works! So it's pretty much like Pick((Ceil(Rand()*3)+RecNo())*3,'Car','Goat','Goat') as DoorPrize ?

Thanks for your time!

Lucas

Not applicable
Author

It works! Could you explain to me what exactly the SubField function does?

Thanks for helping me!

Lucas

tresesco
MVP
MVP

subfield(s, 'delimiter' [ , index ] )

In its three-parameter version, this script function returns a given substring from a larger string s with delimiter 'delimiter'. index is an optional integer denoting which of the substrings should be returned.

rbecher
MVP
MVP

No, you cannot be sure to catch all if you call rand() more than once..

Data & AI Engineer at Orionbelt.ai - a GenAI Semantic Layer Venture, Inventor of Astrato Engine