A random number generator.
Generates a set of random, or if not supported by the operating system, pseudo-random numbers. Each instance creates its own series of numbers.
#include <test.h>
#include <lunchbox/clock.h>
#include <lunchbox/init.h>
#include <lunchbox/rng.h>
#include <servus/uint128_t.h>
#define MAXLOOPS 100000
#define TESTLOOP( type, min, max ) \
{ \
size_t i = MAXLOOPS; \
while( --i ) \
if( rng.get< type >() <= ( min )) \
break; \
TESTINFO( i, "Did never get value below " << (min) << " for " << #type ); \
i = MAXLOOPS; \
while( --i ) \
if( rng.get< type >() >= ( max )) \
break; \
TESTINFO( i, "Did never get value above " << (max) << " for " << #type ); \
{ \
const type value = rng.get< type >(); \
i = MAXLOOPS; \
while( --i ) \
if( rng.get< type >() != value ) \
break; \
TESTINFO( i, "Always get the same value " << value << " for " \
<< #type ); \
} \
}
template< class T > void testSpeed()
{
for( size_t i = 0; i < MAXLOOPS; ++i )
std::cout << float( MAXLOOPS ) *
sizeof( T ) / clock.
getTimef()
<< " byte/ms in " << sizeof( T ) << " byte reads" << std::endl;
}
int main( int argc, char **argv )
{
TESTLOOP( uint8_t, 0, 255 );
TESTLOOP( uint16_t, 50, 65000 );
TESTLOOP( uint32_t, 1<<20, 1u<<12 );
TESTLOOP( uint64_t, 1ull<<52, 1ull<<12 );
TESTLOOP( int8_t, -126, 127 );
TESTLOOP( int16_t, -32000, 32000 );
TESTLOOP( int32_t, -1<<5, 1<<5 );
TESTLOOP( int64_t, -1<<10, 1<<10 );
TESTLOOP( float, 0.1f, 0.9f );
TESTLOOP( double, 0.1, 0.9 );
testSpeed< uint8_t >();
testSpeed< uint16_t >();
testSpeed< uint32_t >();
testSpeed< uint64_t >();
testSpeed< servus::uint128_t >();
return EXIT_SUCCESS;
}