BookmarkSubscribeRSS Feed
s_chanon
Calcite | Level 5
Hello, again.

Can I generate random numbers with specific conditions, like this . . .

"8 set of random numbers (a, b, c, d, e, f, g, and h)"
- each set has the same 'mean' (arithmetic Mean)
- the range of random numbers is 1-9
- every set is normal distribution
- but sample size for each set is difference for example :

set a, sample size 30 (30 of random numbers)
set b, sample size 50
set c, sample size 80
set d, sample size 100
set e, sample size 200
set f, sample size 300
set g, sample size 500
set h sample size 1,000

thank you for your kindness and sorry about my broken english ^_^
3 REPLIES 3
Reeza
Super User
When you say that each set has the same mean, do you mean the exact same mean? 😛

or should all be normal distributed with mean=5, sd=2 for example.

You can look into the rand() function and then either truncate, discard or floor values that go outside of your range, but that may change your distribution. It really depends on what your SD and mean are though...ie a mean of 5 and sd of 1 would rarely have any values outside of 1 or 9.

From all the criteria the sample size being different is actually the easiest to control! Same mean and between 1 and 9 will be the most difficult, in my opinion.
Rambo
Calcite | Level 5
You could try the following code:

data RandomValues;
samplesize = 300; /* update to desired sample size*/
seed = date(); /*setting a seed for the random number generator*/
do i = 1 to samplesize;
obs = ceil((ranuni(seed) * 9)); /*change upper bound here */
output;
end;
run;
Ksharp
Super User
Hi.
I think your biggest problem is to define mu and sigma of Normal Distribution.
Your mean " the range of random numbers is 1-9 " ,That is to mean mu=5, sigma is not quit sure,
because basing my statistical knowledge , the valuefrom normal will 99.7% probability fall in between mu-3*sigma and mu+3*sigma (e.g ~N(0,1) ,the value will 99.7% probability fall in -3 and 3).
So if as the rule of 3*sigma then sigma will be about 1.4, to make sure value fall in 1-9,
I set sigma = 1.2, or you can change it more little.
Remember sigma is relation with mu for your situation ( from 1-9).

Another problem is arithmetic mean,Are you to mean (1+2+3+4)/4 ?
In numeric calculated ,there can not be exactly matched,So I use round() function to
match .1th.


[pre]
%let mu=5; *Normal Distribution 's position parameter;
%let sigma=1.2; *Normal Distribution 's scale parameter;
%let mean=5.0; * Your arithmetic Mean;
%let sample_size=500;


%macro normal(mu= , sigma= , sample_size= , mean=);
data normal_&sample_size ;
value=rand('normal',μ,σ); count+1; sum+value;
output;
do until(count = &sample_size);
value=rand('normal',μ,σ);count+1; sum+value;
if round(sum/count,.1) = &mean then output;
else do;
count+(-1);
sum+(-1)*value;
end;
end;

run;
%mend;

%normal(mu=μ,sigma=σ,sample_size=&sample_size,mean=&mean)


[/pre]



Ksharp

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 892 views
  • 0 likes
  • 4 in conversation