Create 6 numbers random

Solved
Regular Contributor
Posts: 222

Create 6 numbers random

[ Edited ]

Hi,

I need to create 6 number between 01 to 60;

Just like: 45,04,10,54,11,48;

And change each time i run the code.

Any numbers, but without repeating them.

Tks

Accepted Solutions
Solution
‎11-13-2015 11:55 AM
Super User
Posts: 23,776

Re: Create 6 numbers random

[ Edited ]

Theoretically any random number generator has the possibility of duplicates, albeit a small possibility.

Here's a method that would check and ensure it was not a duplicate. There are probably more efficient methods and looking forward to see what others pick.

``````data random;

array s(60) s1-s60 (60*0);
array p(6) p1-p6;

do i=1 to 6;
do while(p(i)=.);
p(i)=floor(rand('uniform')*60+1);
if s(p(i))=0 then s(p(i))=1;
else p(i)=.;
end;
end;

keep p1-p6;
call sortn(of p(*));

run;
``````

All Replies
Super Contributor
Posts: 275

Re: Create 6 numbers random

[ Edited ]
``````data _null_;
do i=1 to 6;
x=round(60*ranuni(123434));
put x;
end;
run; ``````
Solution
‎11-13-2015 11:55 AM
Super User
Posts: 23,776

Re: Create 6 numbers random

[ Edited ]

Theoretically any random number generator has the possibility of duplicates, albeit a small possibility.

Here's a method that would check and ensure it was not a duplicate. There are probably more efficient methods and looking forward to see what others pick.

``````data random;

array s(60) s1-s60 (60*0);
array p(6) p1-p6;

do i=1 to 6;
do while(p(i)=.);
p(i)=floor(rand('uniform')*60+1);
if s(p(i))=0 then s(p(i))=1;
else p(i)=.;
end;
end;

keep p1-p6;
call sortn(of p(*));

run;
``````

Contributor
Posts: 52

Re: Create 6 numbers random

A solution amongst others.

condition 1 -> And change each time i run the code.
condition 2 -> Any numbers, but without repeating them.

data t_a(keep=zRandom);
array xRand(6);
x = 0;
do until (x=6);
y = ceil(60*ranuni(-3));      *** covers condition 1 negative argument ***;
if not(y in xRand) then do; *** covers condition 2 ***;
x+1;
xRand(x)=y;
end;
end;

do  i = 1 to 6;
zRandom = xRand(i);
output;
end;

run;

Posts: 5,541

Re: Create 6 numbers random

[ Edited ]

To select randomly k distinct numbers out of n, use rancomb

``````data test;
array x{60};
retain x (1:60);
retain seed (-1);
do i = 1 to 10;
call rancomb(seed, 6, of x{*});
put x1-x6;
output;
end;
keep x1-x6;
run;
``````

PG
🔒 This topic is solved and locked.