The Museum of HP Calculators

HP Forum Archive 14

[ Return to Index | Top of Index ]

hp 28s, hp48 sx, hp 48gx
Message #1 Posted by elgar on 1 May 2004, 1:06 p.m.

I look for programs that generate (randomly) n natural numbers without repetitions with n greater or equal to 1 and smaller or equal to N (N being a positive integer.) Thank you for your help.

      
Re: hp 28s, hp48 sx, hp 48gx
Message #2 Posted by Vieira, Luiz C. (Brazil) on 1 May 2004, 1:27 p.m.,
in response to message #1 by elgar

Hi, elgar;

All the calculators you mentioned already have a built-in pseudo Random Number generator that uses a seed you can type in. The command to generate a pseudo Random number (to level 1) is [RAND] and the command to "collect" a seed (a real number in Level 1) is [RDZ] (randomize).

Using RPL

You can type in the following program:

« -> N
  « RAND N * IP
  »
»

You can store it with a private name (e.g.: 'MYRAND' [STO]). Also, maybe you probably know about this, but there is no harm mentioning: the [->] symbol refers to the single right-arrow, not a composition of a minus with a greater-than sign. Just for the records and for my own sake...

IP returns the integer part (truncates, no rounding) of the resulting value from RAND (pseudo Random number) times N (your "range"). In this case, N must be in Level 1 prior to execute the program. If you want the program to use an specific N everytime you run it, use only this part:

« RAND N * IP
»

You should store a value in N prior to execute it. Anytime you need to change the range, you simply type the new value for N and press 'N' [STO]. Also, for changing the pseudo sequence, you just type in a real number and execute [RDZ].

Hope this helps.

Luzi (Brazil)

Edited: 1 May 2004, 1:33 p.m.

            
Re: hp 28s, hp48 sx, hp 48gx
Message #3 Posted by elgar on 1 May 2004, 5:38 p.m.,
in response to message #2 by Vieira, Luiz C. (Brazil)

Thank you for your swift help; I look for a program (or programs) that prompts for N and n (n<N) and produces an array (of size n) of randomly chosen integers (all larger or equal to 1 and smaller or equal to N) with no repetitions.

                  
Oops! (was: hp 28s, hp48 sx, hp 48gx)
Message #4 Posted by Vieira, Luiz C. (Brazil) on 1 May 2004, 7:20 p.m.,
in response to message #3 by elgar

Hello, Elgar;

I'm sorry I didn't answer your question, and I realized that I didn't only after reading Veli-Pekka's answer (thank you, Velli-Pekka). I was worried about generating a random sequence instead of taking care for repetition, and I also confused the range with the quantity of numbers needed.

Veli-Pekka actually answered your question. Anyway, if you need help creating the program that does it, let us know.

Thank you and sorry.

Cheers.

Luiz

Edited: 2 May 2004, 1:51 a.m.

            
Re: hp 28s, hp48 sx, hp 48gx
Message #5 Posted by Veli-Pekka Nousiainen on 1 May 2004, 7:00 p.m.,
in response to message #2 by Vieira, Luiz C. (Brazil)

That sounds good.
BUT
If you need to exthaust the 1..N until there are no numbers left, then you could use a list pre-filled with numbers from 1..N
Then you pick one number at time, first 1..N range, remove it , then pick another randomly in the range 1..(N-1), and so on, until the list is empty.
You may gather the results into another list (or vector)
[VPN]

                  
Re: hp 28s, hp48 sx, hp 48gx
Message #6 Posted by elgar on 2 May 2004, 1:58 a.m.,
in response to message #5 by Veli-Pekka Nousiainen

Thank you both for your kind help. I should have specified that n=[k.N], k being a real fraction< or=1/2. I look for a aprogram (or programs) the input of which (variables) are n and N and the outcome (result) is an array (of size n) of randomly generated integers all in the range 1,...,N with no repetitions.

                        
Re: hp 28s, hp48 sx, hp 48gx
Message #7 Posted by Veli-Pekka Nousiainen on 2 May 2004, 4:02 a.m.,
in response to message #6 by elgar

I will give you an example of a code, which is how I first understood the problem.
I hope you have the ability to modify it to your needs.
First:
<< RAND * CEIL >> 'NRAND' STO
Then:
<< -> n
<< n 1. { } -> l
<< DUP2 SWAP
FOR k k
NEXT n ->LIST UNROT
FOR r r NRAND DUP2 DUP2 1
- 1 SWAP SUB UNROT 1 + OVER SIZE
SUB + UNROT GET 'l' SWAP STO+
-1.
STEP DROP l AXL
>> >> >> 'LRAN' STO
'LRAN' RCL BYTES => # 830Dh 166.5
Note:
I wrote this in the wee-wee hours.
It works (for all 1..N) but is not optimized.

You could also use the randomizer to pick up an item to be "sorted" as the first element of a list, then the second, etc.
[VPN]

                              
Re: hp 28s, hp48 sx, hp 48gx
Message #8 Posted by elgar on 2 May 2004, 5:52 a.m.,
in response to message #7 by Veli-Pekka Nousiainen

Thank you very much indeed for your kindness and patience. I have not yet coded it in, I hope it will work.

                                    
Re: hp 28s, hp48 sx, hp 48gx
Message #9 Posted by Veli-Pekka Nousiainen on 2 May 2004, 6:24 p.m.,
in response to message #8 by elgar

Be aware! This "solution" simply picks randomly all numbers between 1..N
[VPN]


[ Return to Index | Top of Index ]

Go back to the main exhibit hall