![]() ![]() ![]()
|
BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1 |
There are three types of classes for generating a random number.
Table 19.1. Random number class
| Class Name | Generating method |
|---|---|
| SFXBrewRandom | Class that generates a random number by using the BREW random number generation function. |
| SFXLCGRandom | Class that generates a random number by using the Linear Congruential Generator method(LCG method). |
| SFXMTRandom | Class that generates a random number by using the Mersenne Twister method(MT method). |
Example 19.2. How to use the SFXBrewRandom class
// generate random number between UINT32_MINIMUM and UINT32_MAXIMUM SInt32 n1 = SFXBrewRandom::GetSInt32(); // generate random number between 0 and UINT08_MAXIMUM UInt08 n2 = SFXBrewRandom::GetUInt08(); // generate random number of Float64 Float64 n3 = SFXBrewRandom::GetFloat64(); // generate random number of Bool type Bool b = SFXBrewRandom::GetBool(); // this method generates random numbers with better quality than the below // Bool b = static_cast<Bool>(SFXBrewRandom::GetUInt32() % 2);
With each numeric type, there is the corresponding random number generating function.
The upper bound of random number value generated by each function is found in macros that shows the maximum and minimum values of each type
Example 19.3. How to use the SFXLCGRandom class
// if seed is not set // set seed for current time SFXLCGRandom random; // generate random number between UINT32_MINIMUM and UINT32_MAXIMUM SInt32 n1 = random.GetSInt32(); random.Randomize(3456); // set seed // generate random number between 0 and UINT08_MAXIMUM UInt08 n2 = random.GetUInt08();
The SFXLCGRandom class generates 48-bit random numbers using the same algorithm as the drand48 function of UNIX.
The same sequence of random numbers is generated for the same seed.
Example 19.4. How to use the SFXMTRandom class
// if seed is not set // set seed for current time SFXMTRandom random; // generate random number between UINT32_MINIMUM and UINT32_MAXIMUM SInt32 n1 = random.GetSInt32(); random.Randomize(3456); // set seed // generate random number between 0 and UINT08_MAXIMUM UInt08 n2 = random.GetUInt08();
The SFXMTRandom class generates random numbers using the Mersenne Twister algorithm.
The same sequence of random numbers is generated for the same seed.
![]() |
Note |
|---|---|
| The method to use the SFXMTRandom class is the same as SFXLCGRandom. | |
![]() |
Memory consumption |
|---|---|
The SFXMTRandom instance consumes about 2.5 kilo bytes memory. Therefore create the SFXMTRandom instance in the heap when insufficient stack capacity. | |
SFXTrigonometric is the class that performs trigonometric operations by using the trigonometric table.
Example 19.5. How to use SFXTrigonometric class
SFXTrigonometric table; // initialize trigonometric table // this processing usually takes time table.Initialize(); SInt32 i; for (i = 0; i < 1000; ++i) { // quick trigonometric calculation Float64 n1 = table.Sin(i); Float64 n2 = table.Cos(i); Float64 n3 = table.Tan(i); // process n1, n2, n3 here }
The conversion functions between radian and degree are available in the SFXTrigonometric class.
The floating point arithmetic operations that can not be used in the standard BREW and the mathematical functions provided with ARM RealView Compilation Tools for BREW V1.2 are available in SophiaFramework.
Example 19.7. Mathematical functions of RealView Compilation Tools for BREW V1.2
// macro (concrete value of macro depends on kinds of compiler) HUGE_VAL INFINITY NAN // functions Float64 acos(Float64) Float64 asin(Float64) Float64 atan(Float64) Float64 atan2(Float64, Float64) Float64 cos(Float64) Float64 sin(Float64) Void __use_accurate_range_reduction(Void) // cannot be used in VC++ Float64 tan(Float64) Float64 cosh(Float64) Float64 sinh(Float64) Float64 tanh(Float64) Float64 exp(Float64) Float64 frexp(Float64, int*) Float64 ldexp(Float64, int) Float64 log(Float64) Float64 log10(Float64) Float64 modf(Float64, Float64Ptr) Float64 pow(Float64, Float64) Float64 sqrt(Float64) Float64 _sqrt(Float64) // in VC++, use sqrt(Float64) Float32 _sqrtf(Float32) // in VC++, use sqrtf(Float64) Float64 ceil(Float64) Float64 fabs(Float64) Float32 _fabsf(Float32) // in VC++, use fabsf(Float32) Float32 fabsf(Float32) Float64 floor(Float64) Float64 fmod(Float64, Float64) Float64 acosh(Float64) // cannot be used in VC++ Float64 asinh(Float64) // cannot be used in VC++ Float64 atanh(Float64) // cannot be used in VC++ Float64 cbrt(Float64) // cannot be used in VC++ Float64 copysign(Float64, Float64) // in VC++, include float.h // and use _copysign(Float64, Float64) Float64 erf(Float64) // cannot be used in VC++ Float64 erfc(Float64) // cannot be used in VC++ Float64 expm1(Float64) // cannot be used in VC++ Float64 finite(Float64) // in VC++, include float.h // and use _finite(Float64) Float64 gamma(Float64) // cannot be used in VC++ Float64 gamma_r(Float64, int*) // cannot be used in VC++ Float64 hypot(Float64, Float64) int ilogb(Float64) // cannot be used in VC++ Float64 isnan(Float64) // in VC++, include float.h // and use _isnan(Float64) Float64 j0(Float64) // in VC++, use _j0(Float64) Float64 j1(Float64) // in VC++, use _j1(Float64) Float64 jn(int, Float64) // in VC++, use _jn(int, Float64) Float64 lgamma(Float64) // cannot be used in VC++ Float64 lgamma_r(Float64, int*) // cannot be used in VC++ Float64 log1p(Float64) // cannot be used in VC++ Float64 logb(Float64) // cannot be used in VC++ Float64 nextafter(Float64, Float64) // in VC++, include float.h // and use _nextafter(Float64, Float64) Float64 remainder(Float64, Float64) // cannot be used in VC++ Float64 rint(Float64) // cannot be used in VC++ Float64 scalb(Float64, Float64) // in VC++, include float.h // and use _scalb(Float64, SInt32) Float64 scalbn(Float64, int) // in VC++, include float.h // and use _sclab(Float64, SInt32) Float64 significand(Float64) // cannot be used in VC++ Float64 y0(Float64) // in VC++, use _y0(Float64) Float64 y1(Float64) // in VC++, use _y1(Float64) Float64 yn(int, Float64) // in VC++, use _yn(int, Float64) // functions used in C++ environment Float64 abs(Float64) Float32 abs(Float32) Float32 acos(Float32) Float32 asin(Float32) Float32 atan(Float32) Float32 atan2(Float32, Float32) Float32 ceil(Float32) Float32 cos(Float32) Float32 cosh(Float32) Float32 exp(Float32) Float32 fabs(Float32) Float32 floor(Float32) Float32 fmod(Float32, Float32) Float32 frexp(Float32, int*) Float32 ldexp(Float32, int) Float32 log(Float32) Float32 log10(Float32) Float32 modf(Float32, Float32Ptr) Float32 pow(Float32, Float32) Float32 pow(Float32, int) Float32 sin(Float32) Float32 sinh(Float32) Float32 sqrt(Float32) Float32 _sqrt(Float32) //in VC++, use sqrt(Float32) Float32 tan(Float32) Float32 tanh(Float32) Float64 pow(Float64, int)
![]() |
include math.h |
|---|---|
When using the mathematical functions of RealView Compilation Tools for BREW V1.2, you don't have to include math.h. | |
|
Copyright (C) 2002 - 2008 Sophia Cradle, Inc. All Rights Reserved. |
![]() ![]() ![]()
|