6

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;

Sanyam Gupta
  • 93
  • 1
  • 1
  • 7

2 Answers2

8

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>.

Dejan Peretin
  • 10,891
  • 1
  • 45
  • 54
1

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/

Rick
  • 1,210
  • 6
  • 11