Functions

/Users/garethloy/Musimathics/Musimat1.2/MusimatChapter9/C090703c.cpp File Reference

#include "MusimatChapter9.h"

Go to the source code of this file.

Functions

 MusimatChapter9Section (C090703c)
Static Void para1 ()
Integer iRandom (Integer L, Integer U)
Static Void para2 ()

Function Documentation

Integer iRandom ( Integer  L,
Integer  U 
)

Definition at line 38 of file C090703c.cpp.

                                      {
        Real rL = L;                                                    // convert L to Real
        Real rU = U + 1.0;                                              // convert U to Real, add 1.0
        Real x = Random(rL, rU);                                // get a Real random value
        Return( Integer(x) );                                   // return result as an Integer
}
MusimatChapter9Section ( C090703c   )

Definition at line 2 of file C090703c.cpp.

References para1(), and para2().

                                 {
        Print("*** Random Integer Numbers Scaled to an Arbitrary Range ***");
        /*****************************************************************************
         
         Random Integer Numbers Scaled to an Arbitrary Range
         
         *****************************************************************************/
        para1(); // Step into this function to continue.
        para2(); // Step into this function to continue.
}
Static Void para1 (  )

Definition at line 13 of file C090703c.cpp.

                    {
        /*****************************************************************************
         We can adapt the Random() function to return integers within a specified 
         integer range. When a real value is converted to an integer, we truncate 
         (discard) the fractional part, leaving the integer part. For example,
         *****************************************************************************/
        
        Print("*** Example of Truncation ***");
        Real r = 3.14159;
        Print("Real r = ", r);
        Integer i = Integer(r);
        Print("Truncated = ", i);
        
        /*****************************************************************************
         prints 3. Truncation is equivalent to the floor function.
         
         The iRandom() function below is a method to generate integer random values over 
         an integer range. It uses the Musimat library Real Random(Real, Real) function 
         to return a Real numerical range, then truncates the result to an integer by type 
         casting the result to type Integer. This behavior is identical to the Musimat
         function Integer Random(Integer, Integer), but is reproduced here for dydactic
         reasons (to keep things simple).
         *****************************************************************************/
}
Static Void para2 (  )

Definition at line 45 of file C090703c.cpp.

References iRandom().

                    {
        /*****************************************************************************
         Note that I added 1.0 to the upper real boundary in iRandom(). Truncation of the random 
         result necessitates slightly increasing the top end of the range of choice. 
         For example, in order to choose a value in the integer range 0 to 9, we must 
         generate a random real value x that lies in the range 0.0 <= x < 10.0. This 
         gives an equal chance of obtaining an integer in the range 0 to 9.
         
         Here are ten values generated by iRandom() over the interval [0,10].
         *****************************************************************************/
        Print("*** Example of Random Integer Values in the Interval [0,10] ***");
        IntegerList x;
        
        For ( Integer i = 0; i < 10; i = i + 1 ) {
                x[i] = iRandom( 0, 10 );
        }
        
        Print( x );
        
}