symport/e32/euser/maths/um_rand.cpp
changeset 1 0a7b44b10206
child 2 806186ab5e14
equal deleted inserted replaced
0:c55016431358 1:0a7b44b10206
       
     1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32\euser\maths\um_rand.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "um_std.h"
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 EXPORT_C TInt Math::Rand(TInt64 &aSeed)
       
    24 /**
       
    25 Generates a stream of uniformly distributed pseudo-random integers
       
    26 in the range, 0 to KMaxTInt.
       
    27 
       
    28 For each stream of pseudo-random numbers you wish to generate, you should
       
    29 pass the reference to the same 64-bit seed on each call to this function.
       
    30 You should not change the seed between calls.
       
    31 
       
    32 @param aSeed A reference to a 64-bit seed, which is updated
       
    33              as a result of the call.
       
    34 
       
    35 @return The next pseudo-random number in the sequence. 
       
    36 */
       
    37 	{
       
    38 
       
    39 	aSeed*=214013;
       
    40     aSeed+=2531011;
       
    41     return(((TInt)(aSeed>>16))&0x7fffffff);
       
    42 	}
       
    43 
       
    44 
       
    45 
       
    46 
       
    47 EXPORT_C TReal Math::FRand(TInt64& aSeed) __SOFTFP
       
    48 /**
       
    49 Generates a stream of uniformly distributed pseudo-random real numbers
       
    50 in the range, 0 to 1.
       
    51 
       
    52 @param aSeed A reference to a 64-bit seed, which is updated
       
    53              as a result of the call.
       
    54 
       
    55 @return The next pseudo-random number in the sequence. 
       
    56 */
       
    57 	{
       
    58 	TUint low = (TUint)Math::Rand(aSeed);
       
    59 	TUint high = (TUint)Math::Rand(aSeed)&0x7FFFFFFF;	// make sure TInt64 is positive
       
    60 	TRealX f((static_cast<TInt64>(high) << 32) | low);	// construct TRealX 0<=f<2^63
       
    61 	if (f.iExp)
       
    62 		f.iExp-=63;						// Scale f to range 0<=f<1
       
    63     return(TReal(f));
       
    64 	}