11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2.1 Nested Procedures • 9> quicksort( a, 1, 5);[1, 2, 3, 4, 5]> eval(a);[1, 2, 3, 4, 5]Maple determines that the A and p variables in the partition subprocedureare defined by the parameter and local variable (respectively)from the outer quicksort procedure and everything works as planned.The variable A can be passed as a parameter to the partition subprocedure(as in the stand-alone partition procedure). However, A does notneed to be passed because, by using Maple scoping rules, it is availableto the inner procedure.Creating a Uniform Random Number GeneratorIf you want to use Maple to simulate physical experiments, you likelyneed a random number generator. The uniform distribution is particularlysimple: any real number in a given range is equally likely. Thus, auniform random number generator is a procedure that returns a randomfloating-point number within a certain range. This section developsthe procedure, uniform, which creates uniform random number generators.The rand command generates a procedure which returns random integers.For example, rand(4..7) generates a procedure that returns randomintegers between 4 and 7, inclusive.> f := rand(4..7):> seq( f(), i=1..20 );5, 6, 5, 7, 4, 6, 5, 4, 5, 5, 7, 7, 5, 4, 6, 5, 4, 5, 7, 5The uniform procedure is similar to rand but returns floating-pointnumbers rather than integers. You can use rand to generate randomfloating-point numbers between 4 and 7 by multiplying and dividing by10^Digits.> f := rand( 4*10^Digits..7*10^Digits ) / 10^Digits:> f();

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!