## generate unique random numbers

Yeah that is my real issue.

I do realize that the RANDOM() function hardly generates same numbers. But, how do I do if I want unique random numbers within a specific range (let say between 1 and 10)?

Best regards,

Vannel,

I do realize that the RANDOM() function hardly generates same numbers. But, how do I do if I want unique random numbers within a specific range (let say between 1 and 10)?

Best regards,

Vannel,

- vzeufack
**Posts:**41**Joined:**Tue Sep 25, 2018 3:52 pm

Vannel,

And I just heard about this function, that does it all:

HTH,

Richard

And I just heard about this function, that does it all:

- Code: Select all
`IMPORT Std;`

ds := DATASET(1000*1000, TRANSFORM({UNSIGNED4 r},

SELF.r := STD.System.Util.GetUniqueInteger()));

COUNT(ds); //a million

out := DEDUP(SORT(ds,r),r);

COUNT(out); //still a million

HTH,

Richard

- rtaylor
- Community Advisory Board Member
**Posts:**1619**Joined:**Wed Oct 26, 2011 7:40 pm

Sorry if I may have not set my question clearly. I think this function generates as many unique integers as you specify right?

I am looking for a function which can generate 10 random numbers taken from the range [1 - 1000] for example. So, the general problem is to generate X random numbers taken from the range [a - b].

I am looking for a function which can generate 10 random numbers taken from the range [1 - 1000] for example. So, the general problem is to generate X random numbers taken from the range [a - b].

- vzeufack
**Posts:**41**Joined:**Tue Sep 25, 2018 3:52 pm

And those X random numbers lying between [a - b] have to be unique.

- vzeufack
**Posts:**41**Joined:**Tue Sep 25, 2018 3:52 pm

vzeufack wrote:Yeah that is my real issue. I do realize that the RANDOM() function hardly generates same numbers. But, how do I do if I want unique random numbers within a specific range (let say between 1 and 10)?

Are you confusing "random" with "shuffled"? Random numbers can repeat. Dice have six numbers on them, and if you throw one die the likelihood of any number showing face-up is 1/6. If you threw one die twice and wound up with "five" both times, that is still random even though you got the same result.

In case you're looking for shuffled numbers, here is one way to do that:

- Code: Select all
`NUM_COUNT := 10;`

ds := DATASET

(

NUM_COUNT,

TRANSFORM

(

{

UNSIGNED4 rand,

UNSIGNED4 num

},

SELF.rand := RANDOM(),

SELF.num := COUNTER

)

);

// sort the dataset by the random numbers

sortedByRandom := SORT(ds, rand);

// strip out the random numbers

onlyNums := TABLE(sortedByRandom, {num});

OUTPUT(onlyNums);

The result will be the unique numbers 1-10 in a random order.

- DSC
- Community Advisory Board Member
**Posts:**571**Joined:**Tue Oct 18, 2011 4:45 pm

15 posts
• Page

**2**of**2**• 1,**2**### Who is online

Users browsing this forum: No registered users and 2 guests