turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- It's possible to generate random numbers with thes...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-24-2011 05:17 AM

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 ^_^

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 ^_^

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-24-2011 12:41 PM

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.

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-25-2011 03:52 PM

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;

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-28-2011 01:06 AM

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

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