Discussion:
programming and loops
(too old to reply)
Marakasmalan
2014-11-23 10:25:00 UTC
Permalink
Good day Everybody

I made an effort to program a random number generator. In short, it should create 4 lists of six unique random numbers, from 1 to 49.

I was hoping somebody may take a look and advise where I have applied the wrong principles or inefficient methods.

<<
0. 'A' STO @reset counter for nr. of duplicate substitutions
1 4 START
1 6 START
RAND 49 * IP 1 + @Random number between 1 and 49
@Is there a difference between FLOOR & IP in this
@application?
NEXT
6 ->LIST SORT @put six numbers to a list and sort
1 5 FOR I @loop to check if numbers are duplicated
DUPDUP @two copies of list on stack for GET commands
I 1 + GET @get nth plus 1 number in list
I GET @get nth number in list
IF == @check if n = (n+1)
THEN
I @placeholder to place new number
RAND 49 * IP 1 + @new random number
PUT @replace duplicate nr. Any difference if I replaced n+1?
SORT @sort list
1 'I' STO @reset FOR-loop for re-checking with new nr
'A' INCR @counter to show number of substitutions
DROP @DROP value INCR placed on stack. Any way to avoid this
@step?
END @end IF loop
NEXT @end FOR loop
NEXT @end START loop
A @put counter value on stack
I'm not sure the above format of the program is correctly written (as far as convention goes) as I am not a programmer.

At this stage the program works and delivers the required results, but I am not sure That I am using the most efficient way with the best suited commands.

Would it be better to put the numbers in an array or is a list the best way?

Regards

Marakas
mjc
2014-11-23 15:13:28 UTC
Permalink
I would create a counter and an initial array of size 49 all zero. The array entrt being zero means that that number has not occurred yet, one means that it has.

Have a loop that creates random numbers. If the number has not occurred yet, enter it into the array and increment the counter. The number can also be stored in an output array. When the counter reaches the desired value, stop.

I see no need for any sorts in your method, also. Just search the output array to see if the value is new.
Uergil
2014-11-24 00:05:53 UTC
Permalink
Post by Marakasmalan
Good day Everybody
I made an effort to program a random number generator. In short, it should
create 4 lists of six unique random numbers, from 1 to 49.
I was hoping somebody may take a look and advise where I have applied the
wrong principles or inefficient methods.
<<
1 4 START
1 6 START
@Is there a difference between FLOOR & IP in this
@application?
NEXT
THEN
@step?
I'm not sure the above format of the program is correctly written (as far as
convention goes) as I am not a programmer.
At this stage the program works and delivers the required results, but I am
not sure That I am using the most efficient way with the best suited
commands.
Would it be better to put the numbers in an array or is a list the best way?
It is a bit simpler to create ONE vector of 24 such numbers and then use

{4 6} @ enter desired new array dimensions, 4 rows by 6 columns
RDM @ redimensions vector to a 4 by 6 matrix
->ROW @ separates rows to into vectors and number of vectors
DROP @ drops the number of vectors leaving only vectors on stack.

For example with
[1 2 3 4 5 6]
{2,3}
on the stack,
RDM produces
[[1 2 3][ 4 5 6]}
->ROW produces
[1 2 3]
[4 5 6]
2
DROP produces
[1 2 3]
[4 5 6]

If you prefer lists, AXL converts both ways,
and EVAL on a list of lists is like ROW-> DROP on an m by n matrix
--
"Ignorance is preferable to error, and he is less
remote from the- truth who believes nothing than
he who believes what is wrong.
Thomas Jefferson
Uergil
2014-11-24 00:43:56 UTC
Permalink
Post by Marakasmalan
Good day Everybody
I made an effort to program a random number generator. In short, it should
create 4 lists of six unique random numbers, from 1 to 49.
I was hoping somebody may take a look and advise where I have applied the
wrong principles or inefficient methods.
Method 1
Make a list or one-dimensional array of all 49 numbers. Shuffle the last
24 members as below then read off the last 24 numbers six at a time.

An effective shuffling program would be to
1. pick one of the first 49 at random and swap it with the one
in position 49 (if it picks the 49th you can skip the actual
switching process)
2. repeat with position 48, position 47, and so on until you have
swapped 24 of them, then read off the last 24 in that list, the ones
that have been randomized, 6 at a time.
--
"Ignorance is preferable to error, and he is less
remote from the- truth who believes nothing than
he who believes what is wrong.
Thomas Jefferson
Joe Horn
2014-11-24 06:30:29 UTC
Permalink
One very minor suggestion: Replace
RAND 49 * IP 1 +
with
RAND 49 * CEIL

BTW, you ask if IP and FLOOR would be the same in this application. The answer is Yes, because the inputs here are always positive. IP and FLOOR only differ when their inputs are negative non-integers.

-Joe-
Werner
2014-11-24 10:00:24 UTC
Permalink
Post by Marakasmalan
Good day Everybody
I made an effort to program a random number generator. In short, it should create 4 lists of six unique random numbers, from 1 to 49.
I was hoping somebody may take a look and advise where I have applied the wrong principles or inefficient methods.
<<
1 4 START
1 6 START
@Is there a difference between FLOOR & IP in this
@application?
NEXT
THEN
@step?
I'm not sure the above format of the program is correctly written (as far as convention goes) as I am not a programmer.
At this stage the program works and delivers the required results, but I am not sure That I am using the most efficient way with the best suited commands.
Would it be better to put the numbers in an array or is a list the best way?
Regards
Marakas
@ Pick K distinct numbers out of 1 till N
@ In: K N
@ Out: List of K distinct numbers, in order
\<<
\-> K N
\<<
1. N FOR I I DUP RAND * CEIL ROLLD NEXT
N K - DROPN
K \->LIST SORT
\>>
\>>

make four lists of 6 numbers out of 1..49:
1. 4. START 6. 49. PICKKN NEXT

Cheers,
Werner
Werner
2014-11-24 10:02:54 UTC
Permalink
@ Pick K distinct numbers out of 1 till N
@ In: K N
@ Out: List of K distinct numbers, in order
PICKKN
\<<
\-> K N
\<<
1. N FOR I I DUP RAND * CEIL ROLLD NEXT
N K - DROPN
K \->LIST SORT
\>>
\>>

make four lists of 6 numbers out of 1..49:
1. 4. START 6. 49. PICKKN NEXT

Cheers,
Werner
Werner
2014-11-25 07:37:46 UTC
Permalink
For K << N, it will be better to just generate numbers and leave out duplicates:

\<<
\-> K N
\<<
{}
1. K FOR I
WHILE
N RAND * CEIL
DUP2 POS
REPEAT DROP
END
+
NEXT
SORT
\>>
\>>

Even for K=6 and N=49, this is twice as fast as the above.

Cheers, Werner

Loading...