source: trunk/src/random.cc @ 237

Last change on this file since 237 was 199, checked in by epyon, 12 years ago
  • GCC64 fix
File size: 1.8 KB
Line 
1// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include "nv/random.hh"
8#include "nv/time.hh"
9
10using namespace nv;
11
12random::random( random::seed_type seed /*= 0 */ )
13        : rng( seed == 0 ? randomized_seed() : seed )
14{
15       
16}
17
18random::seed_type random::randomize()
19{
20        seed_type seed = randomized_seed();
21        rng.seed( seed );
22        return seed;
23}
24
25void random::set_seed( random::seed_type seed /*= 0 */ )
26{
27        rng.seed( seed == 0 ? randomized_seed() : seed );
28}
29
30nv::random& random::get()
31{
32        static random default_rng;
33        return default_rng;
34}
35
36random::result_type random::rand()
37{
38        return rng();
39}
40
41sint32 random::srand( sint32 val )
42{
43        std::uniform_int_distribution<sint32> dist( 0, val - 1 );
44        return dist( rng );
45}
46
47uint32 random::urand( uint32 val )
48{
49        std::uniform_int_distribution<uint32> dist( 0, val - 1 );
50        return dist( rng );
51}
52
53f32 random::frand( f32 val )
54{
55        std::uniform_real_distribution<f32> dist( 0, val );
56        return dist( rng );
57}
58
59sint32 random::srange( sint32 min, sint32 max )
60{
61        std::uniform_int_distribution<sint32> dist( min, max );
62        return dist( rng );
63}
64
65uint32 random::urange( uint32 min, uint32 max )
66{
67        std::uniform_int_distribution<uint32> dist( min, max );
68        return dist( rng );
69}
70
71f32 random::frange( f32 min, f32 max )
72{
73        std::uniform_real_distribution<f32> dist( min, max );
74        return dist( rng );
75}
76
77uint32 random::dice( uint32 count, uint32 sides )
78{
79        std::uniform_int_distribution<uint32> dist( 1, sides );
80        uint32 result = 0;
81        while (count-- > 0)
82        {
83                result += dist( rng );
84        };
85        return result;
86}
87
88random::seed_type random::randomized_seed()
89{
90        return narrow_cast< seed_type >( get_ticks() );
91}
Note: See TracBrowser for help on using the repository browser.