how to generate random integers between 10 and 50 using ranuni() function in sas.
For generating random integer between 1 to 5, I used this code
data random;
do i=1 to 10;
random_int = int(ranuni(0) * 5 + 1);
output;
end;
run;
You can generate a set of random numbers in SAS that are uniformly distributed by using the RAND function in the DATA step or by using the RANDGEN subroutine in SAS/IML software. (These same functions also generate random numbers from other common distributions such as binomial and normal.)
The most common functions to generate random numbers from are the uniform and the normal distribution. This is done with the RAND() function inside a data step, specifying a distribution, and parameters if necessary. RAND('UNIFORM') will provide a random value from 0 to 1.
We simply use the 'Integer' argument and specify the min and max ranges like this. data _null_; r = rand("integer", 1, 10); /* r = rand("integer", 10); equivalent */ put r; run; data _null_; r = rand("integer", 1, 10); /* r = rand("integer", 10); equivalent */ put r; run; Before SAS 9.4M5, we have to do something else.
A seed value specifies a particular stream from a set of possible random number streams. When you specify a seed, SAS generates the same set of pseudorandom numbers every time you run the program.
Using the same logic, you could use:
random_int = int(ranuni(0) * 41 + 10);
ranuni
returns a value from interval <0, 1>
, multiply it by 41 and you get a value from <0, 41>
, add 10 and the final value is from <10, 51>
. Casting to int just cuts off the decimal part so you will never get 51.
Generaly:
a * ranuni(seed) + b;
returns a value from interval <b, a + b>
.
If u is a random uniform variate in [0,1], then x = a + (b-a)*u is random uniform on [a,b]. See http://blogs.sas.com/content/iml/2011/08/24/how-to-generate-random-numbers-in-sas/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With