Tue Aug 20, 2019 3:53 pm
Login Register Lost Password? Contact Us


generate unique random numbers

Comments and questions related to the Enterprise Control Language

Fri Aug 02, 2019 6:33 pm Change Time Zone

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

Fri Aug 02, 2019 6:55 pm Change Time Zone

Vannel,

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
Community Advisory Board Member
 
Posts: 1472
Joined: Wed Oct 26, 2011 7:40 pm

Fri Aug 02, 2019 7:09 pm Change Time Zone

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

Fri Aug 02, 2019 7:11 pm Change Time Zone

And those X random numbers lying between [a - b] have to be unique.
vzeufack
 
Posts: 24
Joined: Tue Sep 25, 2018 3:52 pm

Fri Aug 02, 2019 7:17 pm Change Time Zone

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
Community Advisory Board Member
 
Posts: 560
Joined: Tue Oct 18, 2011 4:45 pm

Previous

Return to ECL

Who is online

Users browsing this forum: No registered users and 1 guest