Date: Thu, 29 Sep 2005 07:20:08 -0500
Reply-To: "Peck, Jon" <peck@spss.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: "Peck, Jon" <peck@spss.com>
Subject: Re: Random number without replacement
Content-Type: text/plain; charset="us-ascii"
It is worth noting that the SPSS Select Cases dialog can, in effect, do this for you. Go to Data/Select Cases/Random Sample of Cases. Choose "exactly m cases from the first n cases. If you have the integers as a variable over those cases, it is doing the same thing as below. By default, the random number generators use a random seed, so it is only necessary to set the seed explicitly if you need the calculation to be exactly repeatable.
If you paste the code from the dialog, you will see a striking resemblance.
Regards,
Jon Peck
-----Original Message-----
From: SPSSX(r) Discussion [mailto:SPSSX-L@LISTSERV.UGA.EDU] On Behalf Of Richard Ristow
Sent: Wednesday, September 28, 2005 10:31 PM
To: SPSSX-L@LISTSERV.UGA.EDU
Subject: Re: [SPSSX-L] Random number without replacement
At 10:26 AM 9/28/2005, Abdus Salam wrote:
>I have a data which contain's Iran's area code and exchange number
>i.e.
>Suppose area code:771 and exchange code:252,253,222
>
>Now my problem is: I want to generate 60 - (4 digit random number's
>) under this exchange code without replacement. Is it possible?
It's possible, and not particularly difficult. There are several
methods. Below is one of the fancier, known as the "K/N" method (you'll
see why, in the code). It's probably fancier than is needed here, but
it does the job neatly, and generates the sample in numerical order.
I've assumed that 0000 is not an acceptable number, and I've generated
10 numbers for each exchange. Sampling is "without replacement" only
within an area code and exchange; I assume that's what you wanted. The
following is tested; SPSS draft output:
"Initialize random number generator": Two steps recommended,
A. Set seed from a source of non-systematic numbers. Don't use the same
seed twice
B. Generate and discard 100 random numbers, after seeding.
List
Notes
|---------------------------|-----------------------|
|Output Created |28 Sep 05 23:21:57 |
|---------------------------|-----------------------|
AREACODE EXCHANGE
771 252
771 253
771 222
Number of cases read: 3 Number of cases listed: 3
* Initialize random-number generator: .
DO IF $CASENUM = 1.
* Seed: trailing four digits of second number on .
* p. 769 of Providence, RI, telephone book for 2005. .
. SET SEED = 1305.
* "Throw away" 100 variates, to bypass anomalous .
* variates that may occur immediately post-seeding .
- LOOP # = 1 TO 100.
. COMPUTE #DISCARD = RV.UNIFORM(0,1).
- END LOOP.
END IF.
COMPUTE #K = 10 /* Desired sample size */.
COMPUTE #N = 9999 /* Population size; must be exact */.
NUMERIC NUMBER (N4).
LOOP NUMBER = 1 TO 9999.
. DO IF RV.UNIFORM(0,1) LE #K/#N.
. XSAVE OUTFILE=SAMPLE.
. COMPUTE #K = #K - 1.
. END IF.
. COMPUTE #N = #N - 1.
END LOOP.
EXECUTE.
GET FILE=SAMPLE.
LIST.
List
Notes
|---------------------------|-----------------------|
|Output Created |28 Sep 05 23:22:00 |
|---------------------------|-----------------------|
AREACODE EXCHANGE NUMBER
771 252 0787
771 252 0819
771 252 0849
771 252 1591
771 252 5511
771 252 6729
771 252 7031
771 252 7535
771 252 7601
771 252 8976
771 253 0375
771 253 1310
771 253 1756
771 253 2405
771 253 2641
771 253 5201
771 253 7170
771 253 7662
771 253 9074
771 253 9572
771 222 0003
771 222 0261
771 222 1904
771 222 2322
771 222 4317
771 222 6632
771 222 6969
771 222 7526
771 222 9424
771 222 9503
Number of cases read: 30 Number of cases listed: 30