Discussion:
random integer from without repeating digits
(too old to reply)
k***@gmail.com
2018-02-17 20:47:04 UTC
Permalink
Hello all,
I have a problem with obtaining a result for MATH - Random integer from 1 to 10. I want to draw without repetitions, 5 different results from those already drawn.

Tks, Tom
Joe Horn
2018-02-18 00:34:19 UTC
Permalink
Post by k***@gmail.com
Hello all,
I have a problem with obtaining a result for MATH - Random integer from 1 to
10. I want to draw without repetitions, 5 different results from those already
drawn.
Tks, Tom
Tom: Which HP calculator model do you have? Different models would do this in different ways.

-Joe-
Joe Horn
2018-02-18 00:48:54 UTC
Permalink
If you have an RPL model, here's a "Selection Without Replacement" program that
I wrote many moons ago. It generates X random integers from 1 to Y. An
interesting feature of this program is that it accomplishes its task with only
one loop, and without any testing for duplicates.

Example: Input 10 and 5, and get a list of 5 random integers between 1 and 10
without any duplicates.

I call it 'DEAL' but you can name it anything you wish.

Needless to say, the two characters "->" stand for the single right-arrow
character, and "<<" and ">>" are the program delimiters. Please note that
there is no space in the "->LIST" command.

<< -> t n
<< 1 t
FOR x x DUP RAND * CEIL ROLLD
NEXT t n - DROPN n ->LIST
-Joe-
Joe Horn
2018-02-18 06:50:13 UTC
Permalink
If you have an HP Prime, rand(N,A,B) returns a list of N random integers between A and B, without duplicates. So rand(5,1,10) returns 5 random integers between 1 and 10.

rand is an undocumented CAS command, so it MIGHT be removed from Prime in a future firmware update. However, it is very unlikely to be removed, since it has been in every firmware version so far.

Note well: rand must be spelled in lowercase letters (*not* RAND).

-Joe-
Veli Pekka Nousiainen
2018-08-15 15:15:05 UTC
Permalink
If you have an HP Prime, rand(N,A,B) returns a list of N random integers between A and B, without duplicates. .
<clip>
...without duplicates ... really? Are you sure?
Joe Horn
2018-08-16 11:47:35 UTC
Permalink
Post by Veli Pekka Nousiainen
If you have an HP Prime, rand(N,A,B) returns a list of N random integers between A and B, without duplicates. .
<clip>
...without duplicates ... really? Are you sure?
Yes, but it only works in CAS view. In Home view, you have to use CAS("rand(N,A,B)").
Joe Horn
2018-08-17 00:04:58 UTC
Permalink
VPN: Aha, you're right, the rand(N,A,B) function contains a bug, whereby for
certain inputs, duplicates can appear in the output. For example, rand(7,1,39)
often gets duplicates in the output, even though it's not supposed to. This bug
has been reported, and MIGHT be removed in some future firmware upgrade. Good
catch!

-Joe-
j***@gmail.com
2018-08-22 00:12:18 UTC
Permalink
Post by Joe Horn
VPN: Aha, you're right, the rand(N,A,B) function contains a bug, whereby for
certain inputs, duplicates can appear in the output. For example, rand(7,1,39)
often gets duplicates in the output, even though it's not supposed to. This bug
has been reported, and MIGHT be removed in some future firmware upgrade. Good
catch!
-Joe-
Indeed. I just tried your example in CAS view, and the first time the list had two 21's!
j***@gmail.com
2018-02-23 20:19:36 UTC
Permalink
Post by k***@gmail.com
Hello all,
I have a problem with obtaining a result for MATH - Random integer from 1 to 10. I want to draw without repetitions, 5 different results from those already drawn.
Tks, Tom
On the Prime, you can also use randperm(10), and select the first 5 digits.

If you have an HP49 or 50, and the ListExt Library (available from hpcalc.org), you can use 10 LSEQ LSHUF 5 LFRST to get the result you requested.

John
Loading...