▼
Posts: 764
Threads: 118
Joined: Aug 2007
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}
▼
Posts: 153
Threads: 7
Joined: Jun 2008
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-
▼
Posts: 113
Threads: 20
Joined: Sep 2013
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!
Posts: 167
Threads: 33
Joined: Jul 2011
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.
▼
Posts: 153
Threads: 7
Joined: Jun 2008
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.
Posts: 109
Threads: 38
Joined: Dec 2012
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...
▼
Posts: 109
Threads: 38
Joined: Dec 2012
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); .
Posts: 153
Threads: 7
Joined: Jun 2008
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.
▼
Posts: 38
Threads: 0
Joined: Aug 2013
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
▼
Posts: 113
Threads: 20
Joined: Sep 2013
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!
▼
Posts: 38
Threads: 0
Joined: Aug 2013
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:-)
▼
Posts: 113
Threads: 20
Joined: Sep 2013
Posts: 709
Threads: 104
Joined: Nov 2005
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?
Posts: 153
Threads: 7
Joined: Jun 2008
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!
|