4

SPSSX Discussion - random number generation

 2 years ago
source link: http://spssx-discussion.165.s1.nabble.com/random-number-generation-td5719063.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Selected post Mar 23, 2013; 7:39pm

random number generation

3 posts
Hi,

I want to generate a set of 250 numbers drawn from the set of integers
[1,2,3,4,5,6,7}. I wonder if there is a simple way to do this in syntax, as
I haven't been able yet to do this with the various RAND. functions, other
than generating real numbers and re-computing them with other functions.

Thanks in advance for help,

Clive

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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
Mar 25, 2013; 1:51pm

Re: random number generation

Administrator
3470 posts
* Use RV.UNIFORM function to generate integers in the range 1-7.
* From the FM:  "The uniform distribution takes values in the range a<x<b".
* Let a=1 and b=8, and then use TRUNC.

new file.
dataset close all.
input program.
loop #i = 1 to 250.
- compute X = trunc(rv.uniform(1,8)).
- end case.
end loop.
end file.
end input program.
frequencies X.


<quote author="Clive">
Hi,

I want to generate a set of 250 numbers drawn from the set of integers
[1,2,3,4,5,6,7}. I wonder if there is a simple way to do this in syntax, as
I haven't been able yet to do this with the various RAND. functions, other
than generating real numbers and re-computing them with other functions.

Thanks in advance for help,

Clive

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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
--
Bruce Weaver
[email protected]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Mar 25, 2013; 2:12pm

Re: random number generation

Administrator
3809 posts
An approach I've adopted lately.

MATRIX.
SAVE (TRUNC(UNIFORM(250,1)*7+1 ))/OUTFILE */VAR UNI7.
END MATRIX.
Bruce Weaver wrote
* Use RV.UNIFORM function to generate integers in the range 1-7.
* From the FM:  "The uniform distribution takes values in the range a<x<b".
* Let a=1 and b=8, and then use TRUNC.

new file.
dataset close all.
input program.
loop #i = 1 to 250.
- compute X = trunc(rv.uniform(1,8)).
- end case.
end loop.
end file.
end input program.
frequencies X.


<quote author="Clive">
Hi,

I want to generate a set of 250 numbers drawn from the set of integers
[1,2,3,4,5,6,7}. I wonder if there is a simple way to do this in syntax, as
I haven't been able yet to do this with the various RAND. functions, other
than generating real numbers and re-computing them with other functions.

Thanks in advance for help,

Clive

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Mar 27, 2013; 7:54pm

Re: random number generation

3 posts
In reply to this post by Clive
Hi Bruce

Thank you very much for your solution to this problem,

Regards,

Clive

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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
Mar 27, 2013; 7:55pm

Re: random number generation

3 posts
In reply to this post by Clive
Hi David

Thank you very much for your matrix solution to this problem,

Regards

Clive

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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
Mar 10, 2022; 6:18pm

Re: random number generation

198 posts
In reply to this post by Bruce Weaver
Hi all,

This code works great.

input program.
loop #i = 1 to 250.
- compute X =rv.univorm (.5,16).
- end case.
end loop.
end file.
end input program.
frequencies X.

Is there a way to tweak this so that it only generates numbers that 0.5 apart?

I need numbers in this range but that only jump by 0.5 so: 0.50, 1.0, 1.5, 2.0, .....15.5, 16.0.

Thanks much!
Carol
Mar 10, 2022; 7:58pm

Re: random number generation

726 posts

One way to do it, truncate to integers over 1-32 [note exactly 33 should never happen in rv.uniform(1,33)], and then divide by 2. Upped the number of simulated draws to show it is approximately is uniform as expected.

*************************.
input program.
loop #i = 1 to 1e6.
- compute X =trunc(rv.univorm (1,33))/2.
- end case.
end loop.
end file.
end input program.
frequencies X.
*************************.

Mar 10, 2022; 8:00pm

Re: random number generation

198 posts
Awesome andy!

This worked like a charm.

Thanks for the quick response!
Mar 11, 2022; 2:13pm

Re: random number generation

Administrator
3470 posts
In reply to this post by Andy W
Here is the same approach but using David's MATRIX method:

MATRIX.
SAVE (TRUNC(UNIFORM(1e6,1)*32+1)/2)/OUTFILE */VAR X.
END MATRIX.
GRAPH HISTOGRAM X.
FREQUENCIES X.
Andy W wrote
One way to do it, truncate to integers over 1-32 [note exactly 33 should never happen in rv.uniform(1,33)], and then divide by 2. Upped the number of simulated draws to show it is approximately is uniform as expected.

*************************.
input program.
loop #i = 1 to 1e6.
- compute X =trunc(rv.univorm (1,33))/2.
- end case.
end loop.
end file.
end input program.
frequencies X.
*************************.
--
Bruce Weaver
[email protected]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Mar 11, 2022; 7:58pm

Re: random number generation

61 posts

Well, I would amend the range "exactly 33 should never happen in rv.uniform(1,33)"
to "never in my lifetime", since the basic Mersenne generator generates in [0,1].


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK