BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
89lilywang89
Fluorite | Level 6

Hi Guys,

 

Is someone can help me out? I need to generate 3 random numbers between 8 to 50, and the sum of these three number must be 50. I really appreciated your help.

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

I think you should think a bit about what distribution you want on those 3 numbers. But one way to do it could be like this:

%let nwant=10; /* how many obs do you want */
%let min=8;
%let max=50;
data test;
  do _N_=1 to &nwant;
    do until(sum(x,y,z)=50);
      x=ceil(rand('UNIFORM',&min,&min+2*(&max-&min)/3));
      y=ceil(rand('UNIFORM',&min,&min+2*(&max-&min)/3));
      z=ceil(rand('UNIFORM',&min,&min+2*(&max-&min)/3));
      end;
   output;
   end;
 stop;
run;

View solution in original post

13 REPLIES 13
Tom
Super User Tom
Super User

I have no idea. 

But since the smallest two numbers possible are 8 and 9 then largest possible number must be 33 and not 50.

89lilywang89
Fluorite | Level 6

Thank you for the reply. The situation is that I need to randomly select 50 observations for my dataset. And there are three groups in the  dataset, I need at least 8 observation from each group. I am thinking to use proc surveyselect to select my sample. So I need "three random number between 8 and 50, and must adding up to 50" using as sampsize.

PaigeMiller
Diamond | Level 26

@89lilywang89 wrote:

 

Is someone can help me out? I need to generate 3 random numbers between 8 to 50, and the sum of these three number must be 50. I really appreciated your help.


 

So if the first random number is 10 and the second random number is 25, then the third number CANNOT be considered random ... and I'm sure you can figure out how to get 10 + 25 + (third number) to equal 50. Is that what you want? Or have I totally misunderstood the question?

 

But the question fails to make any logical sense on another level. Suppose the first random number is 49. Then what? How do you pick two additional random numbers between 8 and 50, such that the total is 50?

--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

@89lilywang89 Hi and welcome to the SAS Community 🙂

 

The randomness of this question seems quite restricted.. What happens if the first number we draw is 49? 

Krueger
Pyrite | Level 9

This isn't right but it'll get you close. Maybe spark some ideas of your own.

 

%let NObs = 10;
data Test(keep=u n1 n2 n3);
call streaminit(123);
a = -1; b = 1;
Min = 8; Max = 34;
do i = 1 to &NObs;
   u = rand("Uniform");            /* decimal values in (0,1)    */
   n1 = min + floor((1+Max-Min)*u); /* integer values in Min..Max */
   n2 = min + floor((1+Max-Min)*u); /* integer values in Min..Max */
   n3 = 50 - n1 - n2;
   output;
end;
run;
s_lassen
Meteorite | Level 14

I think you should think a bit about what distribution you want on those 3 numbers. But one way to do it could be like this:

%let nwant=10; /* how many obs do you want */
%let min=8;
%let max=50;
data test;
  do _N_=1 to &nwant;
    do until(sum(x,y,z)=50);
      x=ceil(rand('UNIFORM',&min,&min+2*(&max-&min)/3));
      y=ceil(rand('UNIFORM',&min,&min+2*(&max-&min)/3));
      z=ceil(rand('UNIFORM',&min,&min+2*(&max-&min)/3));
      end;
   output;
   end;
 stop;
run;
Krueger
Pyrite | Level 9
Other than changing your Max to 34 (as anything above 34 is irrelevant... 8+8 = 16, 50-16 = 34) this looks like a correct way of doing it.
89lilywang89
Fluorite | Level 6

Thanks so much. It works perfectly. 

s_lassen
Meteorite | Level 14

One small note: You may want to tweak the code a bit, if by "between" you meant numbers including 8, which will not be in the solution I posted, because of the CEIL function. If you want the occasional 8 in your output, the easy way may be to change then MIN macro variable to 7.

ballardw
Super User

@89lilywang89 wrote:

Hi Guys,

 

Is someone can help me out? I need to generate 3 random numbers between 8 to 50, and the sum of these three number must be 50. I really appreciated your help.


Are numbers between 8 and 50 limited to integers? If not I don't really see any practical solution (or use).

One way that may give you something to use: generate all the integers between 8 and 50 for 3 variables, add them and keep the totals of 50. Then select a random record. This is one way to accomplish that idea:

data work.start;
   do num1 = 8 to 50;
   do num2 = 8 to 50;
   do num3 = 8 to 50;
      randvalue = rand('uniform');
      if sum(num1,num2,num3)= 50 then output;
   end;
   end;
   end;
run;

proc sort data=work.start;
   by randvalue;
run;

proc print data=work.start (obs=1);
run;

The sort by a random value and using the first sorted order value randomizes the variable combinations.

89lilywang89
Fluorite | Level 6

Thanks so much. It works perfectly.

Amir
PROC Star

Some good points have already been raised by others which will need addressing.

 

I think it might be of help if you explain the context / background to this question, e.g., what are the circumstances under which you need this solution or is it needed as part of a solution to another problem? In which case what is the original problem?

 

Please provide more information, such as:

  1. Can the random numbers be duplicates?
  2. Do the numbers have to be integers? E.g., can 0.1 be used and is there any limit to the number of decimal places?
  3. Is this part of some homework and do you have to use a particular method or function, etc. for practice purposes?

 

 

Amir.

 

 

 

u41054003
Calcite | Level 5
The 3rd number it's not random because it depends on the other 2, to sum 50.
generate two random numbers and the 3rd will be =50-(a+b)

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 1012 views
  • 4 likes
  • 9 in conversation