Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
d44973694
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 (1)
2 Solutions

Accepted Solutions
Gysbert_Wassenaar

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

marcus_sommer

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
Gysbert_Wassenaar

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
d44973694
Contributor II
Contributor II
Author

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+?

marcus_sommer

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