star-sp

Random number generators and distributions
git clone git://git.meso-star.fr/star-sp.git
Log | Files | Refs | README | LICENSE

commit d8d4008a1ebc67489f09e52e230c4ad64846cd14
parent 7adac1eba5298522daa9dec4bef3b821a0f56dc2
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Thu,  2 Apr 2015 12:04:31 +0200

Test and fix the mt19937_64 PRNG

Diffstat:
Mcmake/CMakeLists.txt | 1+
Msrc/ssp_rng.c | 2+-
Asrc/test_ssp_rng_mt19937_64.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -95,6 +95,7 @@ if(NOT NO_TEST) endfunction(new_test) new_test(test_ssp_rng_kiss) + new_test(test_ssp_rng_mt19937_64) endif() ################################################################################ diff --git a/src/ssp_rng.c b/src/ssp_rng.c @@ -207,7 +207,7 @@ ssp_rng_create rng->allocator = allocator; ref_init(&rng->ref); - rng->state = MEM_CALLOC(rng->allocator, 1, sizeof(type->sizeof_state)); + rng->state = MEM_CALLOC(rng->allocator, 1, type->sizeof_state); if(!rng->state) { res = RES_MEM_ERR; goto error; diff --git a/src/test_ssp_rng_mt19937_64.c b/src/test_ssp_rng_mt19937_64.c @@ -0,0 +1,66 @@ +/* Copyright (C) |Meso|Star> 2015 (contact@meso-star.com) + * + * This software is a library whose purpose is to generate [pseudo] random + * numbers and random variates. + * + * This software is governed by the CeCILL-C license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/or redistribute the software under the terms of the CeCILL-C + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL-C license and that you accept its terms. */ + +#include "ssp.h" +#include "test_ssp_utils.h" + +#define NRAND 1024 + +int +main(int argc, char** argv) +{ + struct ssp_rng* rng; + struct mem_allocator allocator; + int i; + (void)argc, (void)argv; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + + CHECK(ssp_rng_create(&allocator, &ssp_rng_mt19937_64, &rng), RES_OK); + CHECK(ssp_rng_set(rng, 0xDECAFBAD), RES_OK); + FOR_EACH(i, 0, NRAND) { + const unsigned long l = ssp_rng_get(rng); + const double d = ssp_rng_get_canonical(rng); + CHECK(l >= ssp_rng_min(rng), 1); + CHECK(l <= ssp_rng_max(rng), 1); + CHECK(d >= 0.0, 1); + CHECK(d < 1.0, 1); + } + + CHECK(ssp_rng_min(rng), 0); + CHECK(ssp_rng_max(rng), UINT64_MAX); + CHECK(ssp_rng_ref_put(rng), RES_OK); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHECK(mem_allocated_size(), 0); + return 0; +}