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