HP Prime Program: Sampling Without Replacement



#2

SAMPLE - Generate a list of random integers from 1 to N using sample without replacement.  

Author: Eddie Shore
Date: 10/1/2013

Syntax: SAMPLE(L, N)
L = length of desired list
N = high integer
If L > N, an error occurs.

Output: random sample

Program:
EXPORT SAMPLE(L,N)
BEGIN
// length, number
LOCAL I, K, T, num;

// error cond
IF L > N THEN
1/0;
END;

// main program
L1:=MAKELIST(0,X,1,L,1);
L1(1):=RANDINT(N-1)+1;
I:= 2;

REPEAT
num:=RANDINT(N-1)+1;
T:=1

// test for uniqueness
FOR K FROM 1 TO I DO
IF num == L1(K) THEN
T:=T + 1;
END;
END;

IF T == 1 THEN
L1(I) := num;
I := I + 1;
END;

UNTIL I == L + 1;
RETURN L1;
END;

Examples:
SAMPLE(5,9) (length of 5, n = 9) can generate:
{5, 4, 8, 2, 6}
{9, 7, 8, 1, 2}
{4, 3, 6, 5, 2}


#3

If instead of a list, an array of random integers from 1 to N (using sample without replacement) is permissible, there's a way to do it in a single command:

rand(N,1,N) [N.B. "rand" must by typed in lowercase letters.]

E.g. rand(6,1,6) --> [4,1,2,6,5,3] (order varies, of course).

The syntax is rand(how many integers you want, lowest integer, greatest integer). If "how many" is greater than greatest-lowest+1, it correctly errors. It's very fast; on the physical calculator it generates a shuffled array of all the integers between 1 and 1000 in 0.126 seconds. The emulator does 10000 in 0.5 seconds on my laptop.

-Joe-


#4

Good to know. It would make sense to add "rand" to the catalog.

Is there something similar to AXL (array to list and vice versa) built into the Prime? Thanks!

#5

Joe, it might be of interest to know timings on other tasks, including benchmark tasks. Is there already a list of such things? Have I missed it? Thanks.


#6

Quote:
Joe, it might be of interest to know timings on other tasks, including benchmark tasks. Is there already a list of such things? Have I missed it? Thanks.

Simple answer: If such a list exists, I hope somebody posts it.

Meta-answer: It would be very easy to create that list, because the built-in time() function returns the execution time of any expression; that's how I timed the rand() examples above:

time(rand(10000,1,10000)) --> 0.223 (seconds, on my tower computer)

Note: In CAS, time() must be spelled in lowercase.

#7

Nice to know about rand(). Is there a way to know about commands that are not in the catalog?

Is it possible to sort the numbers from this sort of output []'s as opposed to {}'s?

Can one concatenate when dealing with []'s?

If so, rand() would shorten my lotto programs which are fairly short...I used sort, delta list, product list to determine if I had repeated numbers...rand() shortens that up, but doesn't seem to lend itself to sorting so that I can have a nice output of increasing numbers, nor does it seem to offer the chance of concatenation where I can have my 5 lotto numbers with the bonus ball labeled and appended to the numbers.

I'm not lotto crazy, but it was a nice introduction to the programming and syntax...


#8

What's also odd is that in CAS, SORT(rand(5,1,56)),rand(1,1,46); yields a nice result, yet it produces a bad argument error in program mode. Actually, a syntax error as stated...bad argument if I had in program, SORT(rand(5,1,56)) and rand(1,1,46); .

#9

Quote:
Is there a way to know about commands that are not in the catalog?
Most of them can be found in the Xcas documentation, but be forewarned: Xcas has roughly twice as many commands as Prime. One way to "cheat" and find out if Prime recognizes a command is to type it in CAS without any arguments. If it is returned as-is, then it is NOT recognized. If it is returned in single quotation marks, or if it does something(!), then it IS recognized.

Examples:
foo --> foo (not recognized)

rand --> 'rand' (recognized!)

log --> 'ln' (recognized in CAS as a natural log!)

Time --> (returns the current clock time in decimal form)

Quote:
Is it possible to sort the numbers from this sort of output []'s as opposed to {}'s?
Yes. SORT() works on arrays just as well as lists:
sort(rand(6,20,25)) --> [20,21,22,23,24,25] (silly example)

Quote:
Can one concatenate when dealing with []'s?
concat([2,3,4],[7,8,9]) --> [2,3,4,7,8,9]

Hope that helps!

-Joe-

Edited: 3 Oct 2013, 9:56 a.m.


#10

If you are interested in timings, there is a shortcut I used and that was not removed: enter your command then tap Sto then press ,
For example
factor(x^100-1) Sto> ,
Some other useful shortcuts Sto> * for factor, Sto> + for partfrac.

About random number, Xcas has now relatively efficient generators for a selection of probability law, some of them should also work on Prime, for example
RANDMAT(100,100,binomial,20,.4) create 10 000 random integers with probability binomial of parameters n=20 and p=.4
RANDMAT(100,100,normald) create 10 000 random floats with probability normal (mu=0, sigma=1)
RANDMAT(100,100,normald,2,.5) same with mu=2 and sigma=.5


#11

What is the current XCAS documentation?

Is it this

http://www-fourier.ujf-grenoble.fr/~parisse/giac/cascmd_en.pdf

or is there something else you recommend?

Thanks!


#12

Quote:
What is the current XCAS documentation?

Is it this

http://www-fourier.ujf-grenoble.fr/~parisse/giac/cascmd_en.pdf

or is there something else you recommend?

Thanks!


This is the best documentation that we have in English. If you can read French, the French documentation is much more complete (and if you read Greek, there is also a translation in Greek). I would be glad to find people writing doc for Xcas/Prime CAS in English by the way:-)

#13

Merci de me répondre!

#14

Quote:
If you are interested in timings, there is a shortcut I used and that was not removed: enter your command then tap Sto then press ,
For example
factor(x^100-1) Sto> ,
Some other useful shortcuts Sto> * for factor, Sto> + for partfrac.

About random number, Xcas has now relatively efficient generators for a selection of probability law, some of them should also work on Prime, for example
RANDMAT(100,100,binomial,20,.4) create 10 000 random integers with probability binomial of parameters n=20 and p=.4
RANDMAT(100,100,normald) create 10 000 random floats with probability normal (mu=0, sigma=1)
RANDMAT(100,100,normald,2,.5) same with mu=2 and sigma=.5


It seems to me the "power" behind the HP Prime is partly locked away in CAS environment. I think a lot of folks are anxiously awaiting for the moment when creating CAS programs will be as easy as creating non-CAS ones. Is this something that would take a while to implement, though?

#15

Quote:
enter your command then tap Sto then press , For example factor(x^100-1) Sto> ,
STORE COMMA to launch a timer... but of course! The hidden timer in the HP-45 was also launched via an undocumented key combination! But in that case it was RCL CHS+7+8. Thanks, Bernard!

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending little images to the Prime (...and Program name bug?) Erwin Ried 19 7,480 12-10-2013, 05:35 PM
Last Post: Erwin Ried
  Problem with debug HP prime program, somebody help me? Carlos CM (Mexico) 6 3,375 12-05-2013, 03:07 PM
Last Post: Carlos CM (Mexico)
  HP PRIME: APP program code DISAPPEARS !! Joseph Ec 0 1,220 11-25-2013, 11:35 AM
Last Post: Joseph Ec
  Prime: Placing more than 1 item on the RPN stack in a single program? John Colvin 4 2,274 11-19-2013, 08:59 AM
Last Post: Miguel Toro
  Prime: Program size limited to 64K? Erwin Ried 4 2,057 11-17-2013, 11:42 PM
Last Post: Joseph Ec
  [HP-Prime] Picking elements from a List in a program Jean-Michel 3 2,026 11-15-2013, 04:16 AM
Last Post: dg1969
  HP Prime: Recommendation for future RPN Program Mode BruceTTT 3 2,181 11-13-2013, 10:03 PM
Last Post: BruceTTT
  HP Prime: run a program in another program Davi Ribeiro de Oliveira 6 2,734 11-11-2013, 08:28 PM
Last Post: Davi Ribeiro de Oliveira
  [prime] Insert a breakpoint in a program for debugging ? Olivier Lecluse 3 1,806 11-09-2013, 09:41 PM
Last Post: Joseph Ec
  HP PRIME: Hide return value from program and swap Edit with Run vrrr 2 1,558 11-09-2013, 04:04 PM
Last Post: vrrr

Forum Jump: