Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
fkeuroglian
Partner - Master
Partner - Master

Rand Function

Hi, experts, i would like to know if there is a way to use the rand function but i pass the posible value, not only 1 or 0

i want to create random data but i provide the possible numbers, only create rando using that list of numbers

thank you a lot

Fernando

2 Replies
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

Rand() only returns a numbers between 0 and 1. You can use the result of rand to select from a list of numbers using various arithmetic and rounding operations. Some examples:

=ceil(rand()*1000,100)   // Generate a number between 100 and 1000 in multiples of 100

=pick(ceil(rand()*5), '12', '15', '27', '3', '44')   // Picks randomly from these 5 values.

-Rob

simenkg
Specialist
Specialist

I suppose you can do something like this.

mapNumbers:

mapping load * inline [

ID, Number

1, 3333

2, 5555

3, 1337

4, 5433

5, 11

6, 5432

7, 1223

8, 332

];

let vNoOfNumbers = NoOfRows('MapNumbers');

Fact:

load
applymap('mapNumbers',ceil(rand()*'$(vNoOfNumbers)'),null()) as RandomSelectedNumber

From

.

.

.

If you want to read the possible numbers from a file you would do:

mapNumbers:

mapping load rowno() as ID, Number from File.