Moodle, STACK and Maxima: Random Variables

The rand() function is STACK’s implementation of Maxima’s random() function.  Random variables are zero-numbered. You may need to use simplification to reduce the output to a single value.

x:rand(5);            /* pick a random number between 0 and 4 */
y:ev(rand(5)+1,simp); /* pick a random number between 1 and 5 */

NB: if the simplification is off then the result of y would be 4+1

The rand() function in STACK also picks from a list:

x:rand([a,b,c,d]);                   /* pick a random item from a list. 
                                        This list shows lower case letters */
y:ev((rand(5)+1)*rand([1,-1]),simp); /* pick a random positive or negative
                                        number between 1 and 5 */

You can apply this behaviour of the rand() function to any list object. For example

w:makelist(x,x,1,10);          /* make a list of integers between 1 and 10 */
x:makelist(2^x,x,1,8);         /* make a list of powers of 2 from 2^1 to 2^8 */
y:primes(2,30);                /* use the primes() function to generate a
                                  prime number between 2 and 30. */
z:map(fib,makelist(x,x,1,10)); /* use the map() and fib() functions to make a 
                                  list of the first 10 Fibonacci numbers */

NB: not all Maxima functions are allowed in STACK, but they can be enabled by MSD LT team if needed.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.