Mon Dec 09, 2019 12:37 pm

## generate unique random numbers

Comments and questions related to the Enterprise Control Language
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,
vzeufack

Posts: 24
Joined: Tue Sep 25, 2018 3:52 pm

Vannel,

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 Posts: 1488
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].
vzeufack

Posts: 24
Joined: Tue Sep 25, 2018 3:52 pm

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

Posts: 24
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 numberssortedByRandom := SORT(ds, rand);// strip out the random numbersonlyNums := TABLE(sortedByRandom, {num});OUTPUT(onlyNums);`

The result will be the unique numbers 1-10 in a random order.
DSC Posts: 566
Joined: Tue Oct 18, 2011 4:45 pm

Previous