BookmarkSubscribeRSS Feed
HeidiDT
Quartz | Level 8

Hi All

I am hoping someone has come across this issue previously and knows why it happens: I am assigning a random number between 1 and 1,000 to a daily feed of data by making use of the RANUNI function:

                SAS_RANDOM_NUMBER  = int(1000*ranuni(-1));

The problem I have is that on certain days the same number is allocated to 90% of the records, so not so random after all. Has anyone ever come across this problem? 

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Try using the Rand Function instead. 

 

Rick Wicklin explains why in the article Six reasons you should stop using the RANUNI function to generate random numbers.

Kurt_Bremser
Super User

Don't use RANUNI, use the improved RAND:

call streaminit(-1);
SAS_RANDOM_NUMBER  = int(1000*rand('uniform'));

If you want a uniform integer distribution, use

rand('integer',1000);
HeidiDT
Quartz | Level 8

Thanks guys! Will that explain why more than 90% of the records get the same number assigned though? Is RANUNI that "bad"?

Kurt_Bremser
Super User

Well, I never experienced something as egregious as what you describe, but then I rarely needed random numbers. I do mostly ETL work, no simulations.

And I've been using RAND since it was introduced.

Watts
SAS Employee

It looks like you're specifying a seed value of -1, which uses the time of day to initialize the seed stream. I don't know how you're using the RANUNI function to generate the list of random numbers, but if (?) you're including the function in a loop, it's possible that this runs faster than the clock updates -- and therefore reuses the same seed.

 

For more information, see How to Generate Random Numbers in SAS by @Rick_SAS .

 

 

Rick_SAS
SAS Super FREQ

If you post the code you are using, we can say more about what you are seeing and why.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 1668 views
  • 0 likes
  • 5 in conversation