Qlik Community

New to Qlik Sense

Discussion board where members can get started with Qlik Sense.

Announcements
Uploads getting stuck in the virus scanner. We are investigating.
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Contributor II
Contributor II

Autogenerate and Rand function in Qlik

what does the below script do? How many iterations would it execute?

TempTable:

Load
Pick(Ceil(3*Rand()),'Standard','Premium','Discount') AS ProductType,
Pick(Ceil(6*Rand()),'Apple','Orange','Cherry','Plum','Fig', 'Pear') AS Category,
Pick(Ceil(3*Rand()),'Heavy','Medium','Light') AS Weight,
Pick(Ceil(2*Rand()),'2013','2014') AS Year,
Round(1000*Rand()*Rand()*Rand()) AS Sales,
Rand() as RandValue,
IterNo() as IterValue
Autogenerate 20  
While IterNo()=1 or Rand()<=0.5; /*if I remove the where clause, it will run 20 times per the autogenerate function. Why doesn't it stop on 1st iteration since we have condition IternO()=1 */

Thanks in advance!

Labels (2)
2 Solutions

Accepted Solutions
Highlighted
MVP & Luminary
MVP & Luminary

It doesn't necessarily stop after one iteration because you have added a second condition with the OR operator. So the while loop continues if iterno = 1 or as long as the result of rand() is smaller or equal to 0.5. So you get at least one iteration and potentially many iterations.


talk is cheap, supply exceeds demand

View solution in original post

Highlighted
MVP & Luminary
MVP & Luminary

Iterno() starts always with 1 and will be never zero. I think I wouldn't use this kind of logic if I would want to create a random number of records between a defined min/max-value else rather playing with something like this:

...
autogenerate rangemax(rangemin(30, rand()*300), rand()*3000);

- Marcus

View solution in original post

3 Replies
Highlighted
MVP & Luminary
MVP & Luminary

It doesn't necessarily stop after one iteration because you have added a second condition with the OR operator. So the while loop continues if iterno = 1 or as long as the result of rand() is smaller or equal to 0.5. So you get at least one iteration and potentially many iterations.


talk is cheap, supply exceeds demand

View solution in original post

Highlighted
Contributor II
Contributor II

But in that case, I will get 20 records for 1st iterations as the IterNo() is zero.

I will have another 40 records as Iterno() is 1 irrespective of the RAND() value. So total 40 records at least because one of the condition will hold true.

The 3rd loop will generate records until the RAND() returns a value >0.5.

However, I sometime get only 30-35 records. Why is that happening? Shouldn't  it be always 40+?

Highlighted
MVP & Luminary
MVP & Luminary

Iterno() starts always with 1 and will be never zero. I think I wouldn't use this kind of logic if I would want to create a random number of records between a defined min/max-value else rather playing with something like this:

...
autogenerate rangemax(rangemin(30, rand()*300), rand()*3000);

- Marcus

View solution in original post