LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (September 2010)Back to main SPSSX-L pageJoin or leave SPSSX-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Mon, 27 Sep 2010 12:51:53 -0700
Reply-To:   Albert-Jan Roskam <>
Sender:   "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From:   Albert-Jan Roskam <>
Subject:   Re: generating random (not consecutive) identification numbers
Comments:   To: Justin Carroll <>
In-Reply-To:   <>
Content-Type:   multipart/alternative;


Why make completely random numbers? For bank numbers and ssn, often a mechanism is used to minimize the risk of typos. That is, typos often yield a non-existent code. Below is a little program to generate numbers that are "modulus 11 proof" There are various ways to do this ( I used a 'control digit' method). Check the code because I just cobbled it together.

Cheers!! Albert-Jan

import os

def isValidSsn(ssnNum, nDigits = 9): ssn = str(ssnNum).zfill(nDigits) sigma = 0 for n, digit in enumerate(ssn[:-1]): sigma += (len(ssn) - n) * int(digit) check = int(ssn[-1]) if sigma % 11 == check: return True else: return False

def makeSsns(outfile = "c:/temp/ssn.txt", n = 20, offset = 10000): with open(outfile, "wb") as f: nSsn = 0 ssn = 0 + offset while True: ssn += 1 sigma = 0 if isValidSsn(ssn): nSsn += 1 f.write(str(ssn).zfill(9) + os.linesep) if nSsn == n: break

makeSsns(n = 20)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

________________________________ From: Justin Carroll <> To: SPSSX-L@LISTSERV.UGA.EDU Sent: Mon, September 27, 2010 8:19:33 PM Subject: Re: [SPSSX-L] generating random (not consecutive) identification numbers


I'll respond with some Python (and maybe pure sytanx ways) a bit later today (assuming I remember!!), but I often direct my students to use This website uses "atmospheric static" to generate seemingly random numbers (it uses static in addition to some choice algorithms which plays on the age-old argument if true-randomization even exists). As such, there are some options via this website ( which allow you create upwards to millions of cases (or so I think I remember seeing) of consecutive random numbers (aka non repeating numbers). You can also specific the range so if you a 6 digit random non repeating ID, you can select 100,000-999,999, and it'll give a list of random non-repeating numbers. Furthermore, I believe there is an export option (if not copy and paste often works well enough for smaller generations).

This is quick, dirty, and I think just as good as using any randomization techniques that SPSS or Python can derive.

In writing this, I can see a process in which you generate the $casenum, as you've done in the past, and use a random algorithm in SPSS (pure syntax) to select from the available cases you already generated (thus picking from the set of numbers at random, and reordering them based on their ordinal selection: aka the first case selected might be 642,324 which goes at the top of the column). Interesting thought! I'll play around with it tonight/tomorrow (hopefully!) and get back to you if one of the listers doesn't respond with a more cogent response.

Check out random.orgthough (specifically, I don't see why this couldn't work in your situation as a quick solution (specifics of your situation notwithstanding).


J. R. Carroll Grad. Student in Pre-Doc Psychology at CSUS Research Assistant for Just About Everyone. Email: -or- Phone: (916) 628-4204

On Mon, Sep 27, 2010 at 11:04 AM, Debbie Hahs-Vaughn <> wrote:

I have used COMPUTE ID=$CASENUM to generate consecutive identification >numbers. However how can I generate identification numbers that are not >consecutive but just purely random? I am trying to avoid having numbers >that are only one digit difference in the ID so that there is less room for >error in what we are trying to do. Thanks! > >===================== >To manage your subscription to SPSSX-L, send a message to >LISTSERV@LISTSERV.UGA.EDU (not to SPSSX-L), with no body text except the >command. To leave the list, send the command >SIGNOFF SPSSX-L >For a list of commands to manage subscriptions, send the command >INFO REFCARD >


Back to: Top of message | Previous page | Main SPSSX-L page