Announcements
You can succeed best and quickest by helping others to succeed. Join the conversation.
cancel
Showing results for
Did you mean:
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:
ROUND(rand()*20,1)AS User_Number,
'10' AS Percentage
AutoGenerate 2;

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

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

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

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

Thanks

Varun

Labels (1)
• ### General Question

1 Solution

Accepted Solutions
MVP

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:
ROUND((rand()*19)+1,1) as value
Autogenerate 2000;

UserCodes:
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:
Lookup('UserCode', 'RowNumber', RowNo(), 'UserCodes') as User_Number,
'10' AS Percentage
AutoGenerate 2;

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

DROP table UserCodes;``````

2 Replies
MVP

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:
ROUND((rand()*19)+1,1) as value
Autogenerate 2000;

UserCodes:
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:
Lookup('UserCode', 'RowNumber', RowNo(), 'UserCodes') as User_Number,
'10' AS Percentage
AutoGenerate 2;

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

DROP table UserCodes;``````

Specialist II
Author

Thanks a lot.. it worked.

Tags
Community Browser