## uniformInt : Why does not this function produce different numbers?

User
Posts: 15
Joined: 2 years ago

### uniformInt : Why does not this function produce different numbers?

Hi everyone

I want to pick 10 number randomly , from a set of i /1*300/.
i use this code :

Code: Select all

``````Set       I     /0*300/
picks /p1*p10/;
Scalar    pick;
Parameter MyParameter(I);

MyParameter(I) = 0;
loop(picks,
pick = uniformInt(1, card(I));

* Make sure to not pick the same one twice
while(sum(I\$(pick=ord(I)),MyParameter(I))=1,
pick = uniformInt(1, card(I))
Display 'here';
);

MyParameter(I)\$(pick=ord(I))=1;
);
Display MyParameter;
``````
I want to run this code a few times ,And I want to choose 10 random numbers at first time,
Second time 20, Second time 30, and ... Tue 10 times 100 random numbers.
In addition, I need to select new numbers every time,
i mean , the numbers that are selected the second time must be different from numbers that are selected the first time.

but uniformInt Every time,selects repetitive numbers.
For example, the results for the selection of 10 and 20 are as follows:

Code: Select all

``````* for      picks /p1*p10/;
21  1.000,    52  1.000,    68  1.000,    88  1.000,    91  1.000,    106 1.000
151 1.000,    166 1.000,    254 1.000,    258 1.000
``````

Code: Select all

``````* for      picks /p1*p20/;

21  1.000,    40  1.000,    49  1.000,    52  1.000,    68  1.000,    76  1.000
88  1.000,    91  1.000,    106 1.000,    132 1.000,    151 1.000,    166 1.000
175 1.000,    193 1.000,    202 1.000,    230 1.000,    254 1.000,    258 1.000
299 1.000
``````
What should I do to avoid duplicate numbers every time?
Why does not this function produce different numbers?

Best,

cbhomia
User
Posts: 8
Joined: 2 years ago

### Re: uniformInt : Why does not this function produce different numbers?

Hi,

The uniformInt is working fine. Look at the indices for MyParameter(I). When I ran the code, the indices were all random, but the values were all 1 because you assigned the value 1 in the code.

Code: Select all

``MyParameter(I)\$(pick=ord(I))=1;``
Also, as I understand your process, you want to have 10 loops with number of integers in the series 10,20,...100 . And you want them to be unique over the whole process. If that is correct, you will need more numbers than 300 (550 to be exact).

Now, coming you generating the random variables. Your code above randomly chooses a value of I, and makes MyParameter(I) a non zero. Hence, your values in MyParameter(I) are 1. You have explicitly done that.

What I think you intended to do was assign random values to series p1,..p10. I believe you defined MyParameter over wrong domain. Look at the code below.

Code: Select all

``````Set       I     /1*300/
picks /p1*p10/;
Scalar    pick;
Parameter MyParameter(picks);

loop(picks,
pick = uniformInt(1,card(I));
MyParameter(picks) = pick;

);

Display MyParameter;
``````

You will see that you have 10 random values in MyParameter(picks). Hope this helps you get started.

User
Posts: 15
Joined: 2 years ago

### Re: uniformInt : Why does not this function produce different numbers?

Hi cbhomia , thanks . but,
I define Myparameter because I put my selected number on it .
Then Myparameter should be as I defined.

In fact

Code: Select all

``````* for      picks /p1*p10/;
21  1.000,    52  1.000,    68  1.000,    88  1.000,    91  1.000,    106 1.000
151 1.000,    166 1.000,    254 1.000,    258 1.000
``````
21,52,68,88,91,106,151,166,254,258 is important for me.

When I ran code to select 20 number , I had the following result.

Code: Select all

``````* for      picks /p1*p20/;

21  1.000,    40  1.000,    49  1.000,    52  1.000,    68  1.000,    76  1.000
88  1.000,    91  1.000,    106 1.000,    132 1.000,    151 1.000,    166 1.000
175 1.000,    193 1.000,    202 1.000,    230 1.000,    254 1.000,    258 1.000
299 1.000
``````
But , my question is why I have
21,52,68,88,91,106,151,166,254,258 again in above?

What should I do that I have different number?

I have 300 numbers , from 1 till 300 , I={1,2,..300} . I want to select 10 numbers randomly from set I .

how can Select 10 random element of a set ' I' , without repetition? And next time I need to select 20 random number ,in addition these choosed number must be different from last selected.

cbhomia
User
Posts: 8
Joined: 2 years ago

### Re: uniformInt : Why does not this function produce different numbers?

The numbers which are important to you (21,52,68... etc) are appearing as an index of MyParameter with non-zero value, rather than value in your parameter MyParameter(I).
Per your code, for the randomly generated values 'pick' , you are essentially doing the following

Code: Select all

``````pick == some I from the set /1*300/
MyParameter(pick) =1
``````
All other values in the MyParameter(pick) are zero.

On the other hand, what you really want (as per your description) is

Code: Select all

``````picks /p1*p10/
pick ==some I from the set /1*300/
Myparameter(p1) = pick ;
``````
I'm sure you can see the difference.

Duplicate Values:
Im guessing you run the model again after re-defining the set Picks /p1*p10/ to set Picks /p1*p20/ .
Now, when you run the model again, the information of the previous run is lost. Your logic (in current form) only works to ensure that for each cycle of P's , you get unique value.

For each cycle of selection (i.e. N=10, N=20), you would need to, in a way redefine the set or the parameter.
Thus, it would be futile to work on a logic until you decide what do you want to change? Indices or the values ?

Regards,
cbhomia