SPSS generate random number (decimal and whole number)

This SPSS tutorial explains how to generate random number in SPSS (decimal and whole number).

You may also want to read:

Excel VBA generate non-duplicated random number

Assign unique random number using Excel

SPSS generate random number (decimal number)

Assume that we have 10 data from 1 to 10.

SPSS generate random number 01

 

Navigate to Transform > Compute Variable

SPSS generate random number 02

 

On the left hand side, type a name for the Target Variable, which is the new variable that contains the random number to be generated. In this example, I call the variable random_number.

On the right hand side, type the formula RV.UNIFORM(1,10) in the Numeric Expression box. You can read the description of RV.UNIFORM Function, which returns a random number with specific range where the random number is a uniform distribution (simply speaking, all values have equal probability).

Click on OK

SPSS generate random number 04

 

Go back to Data View, a new variable called random_number is created, which contains 10 random numbers (because we have 10 data in the data sheet). Although by default the random numbers have 2 decimal places, you can expand to maximum 16 decimal places in Variable View. The random numbers should not be duplicated.

SPSS generate random number 05

 

SPSS generate random number (integer / whole number)

In the last example, we generate random numbers with decimal places, in this section we will demonstrate how to generate random integer.

Navigate to the Compute Variable box again, this time we create another variable called random_number2 and use function TRUNC to cut the decimal places.

SPSS generate random number 06

 

Go to Data View, the numbers in random_number2 variable are all integers.

SPSS generate random number 07

You can hardly see the upper limit 10 after using TRUNC because you need the random number to be exactly 10, there is no value between 10 and 11 to be truncated as 10. You can consider adding 1 to the upper limit as

=TRUNC(RV.UNIFORM(1,11))

But there is still a chance you will see 11.

It you are just trying to randomly select some sample data, consider using Select Cases instead.

 

Wyman W
Wyman is a Business Analyst based in Hong Kong, specialized in creating custom Function and Sub solutions, and is proficient in report automation with Access.

He is also a:
- Microsoft Most Valuable Professional (Excel)
- Microsoft Community Contributor
- Microsoft Office Specialist
- Microsoft Specialist
- Microsoft Technical Associate
- Microsoft Certified Professional
- IBM SPSS Specialist

Leave a Reply

Your email address will not be published. Required fields are marked *