1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     math_Rand.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46347 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn/math.h>
17 
18 #include <nn/math/math_Rand.h>
19 #include <nn/types.h>
20 
21 namespace nn { namespace math {
22 
SetSeed(u64 seed)23 void RandomGenerator::SetSeed(u64 seed)
24 {
25     m_x = seed;
26     m_mul = (1566083941LL << 32) + 1812433253LL;
27     m_add = 2531011;
28 }
29 
Generate(u32 max)30 u32 RandomGenerator::Generate(u32 max)
31 {
32     m_x = m_mul * m_x + m_add;
33     if (max == 0)
34     {
35         return static_cast<u32>(m_x >> 32);
36     }
37     else
38     {
39         return static_cast<u32>(((m_x >> 32) * max) >> 32);
40     }
41 }
42 
43 // Function definition of private RandomGenerator class.
SetSeed(u64 seed)44 void RamdomGenerator::SetSeed(u64 seed)
45 {
46     m_x = seed;
47     m_mul = (1566083941LL << 32) + 1812433253LL;
48     m_add = 2531011;
49 }
50 
Generate(u32 max)51 u32 RamdomGenerator::Generate(u32 max)
52 {
53     m_x = m_mul * m_x + m_add;
54     if (max == 0)
55     {
56         return static_cast<u32>(m_x >> 32);
57     }
58     else
59     {
60         return static_cast<u32>(((m_x >> 32) * max) >> 32);
61     }
62 }
63 
64 
65 }}  // math
66