Skip to main content
Announcements
Live today at 11 AM ET. Get your questions about Qlik Connect answered, or just listen in. SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
techvarun
Specialist II
Specialist II

How to generate random numbers between 1-20

I want to generate 20 random numbers to be distributed within 100%.

 

I am using the below script but it is creating some duplicates. Please  advice on getting the unique 20 records

 

Table:
LOAD
ROUND(rand()*20,1)AS User_Number,
'10' AS Percentage
AutoGenerate 2;


Concatenate(Table)
LOAD
ROUND(rand()*20,1)AS User_Number,
'15' AS Percentage

AutoGenerate 3;
Concatenate(Table)
LOAD
ROUND(rand()*20,1)AS User_Number,
'20' AS Percentage
AutoGenerate 4;

Concatenate(Table)
LOAD
ROUND(rand()*20,1)AS User_Number,
'25' AS Percentage
AutoGenerate 5;

Concatenate(Table)
LOAD
ROUND(rand()*20,1)AS User_Number,
'30' AS Percentage
AutoGenerate 6;

 

Thanks

Varun

 

 

Labels (1)
1 Solution

Accepted Solutions
rubenmarin

Hi, you can create a table with enough records to ensure it has all numbers, and from this table load the ones that are different.

rand:
LOAD
ROUND((rand()*19)+1,1) as value
Autogenerate 2000;

UserCodes:
LOAD value as UserCode,
	RowNo() as RowNumber
resident rand
Where not exists('UserCode',value);
DROP Table rand;

Then you can use the Lookup() function to assing the values of this table:

Table:
LOAD
Lookup('UserCode', 'RowNumber', RowNo(), 'UserCodes') as User_Number,
'10' AS Percentage
AutoGenerate 2;

Concatenate(Table)
LOAD
Lookup('UserCode', 'RowNumber', RowNo(), 'UserCodes') as User_Number,
'15' AS Percentage
AutoGenerate 3;
...

DROP table UserCodes;

 

View solution in original post

2 Replies
rubenmarin

Hi, you can create a table with enough records to ensure it has all numbers, and from this table load the ones that are different.

rand:
LOAD
ROUND((rand()*19)+1,1) as value
Autogenerate 2000;

UserCodes:
LOAD value as UserCode,
	RowNo() as RowNumber
resident rand
Where not exists('UserCode',value);
DROP Table rand;

Then you can use the Lookup() function to assing the values of this table:

Table:
LOAD
Lookup('UserCode', 'RowNumber', RowNo(), 'UserCodes') as User_Number,
'10' AS Percentage
AutoGenerate 2;

Concatenate(Table)
LOAD
Lookup('UserCode', 'RowNumber', RowNo(), 'UserCodes') as User_Number,
'15' AS Percentage
AutoGenerate 3;
...

DROP table UserCodes;

 

techvarun
Specialist II
Specialist II
Author

Thanks a lot.. it worked.