c - TinyMT example Initialisation, Usage -
i'm having problem c code when using tinymt. wanting explanation on how functions work , want wanting parameters.
my linter throwing errors :(
#include <stdio.h> #include <stdlib.h> #include "tinymt64.h" int main(void){ int r; tinymt64_init(r,100) tinymt64_generate_uint64(r); return 0; }
tinymt library being used randomly generate numbers, there no documentation on how use this, love if give me directions on how use fantastic tool!
thank in advance
let's @ comments in source code:
/** * function initializes internal state array 64-bit * unsigned integer seed. * @param random tinymt state vector. * @param seed 64-bit unsigned integer used seed. */ void tinymt64_init(tinymt64_t * random, uint64_t seed)
the corresponding header file says:
/** * function outputs 64-bit unsigned integer internal state. * @param random tinymt internal status * @return 64-bit unsigned integer r (0 <= r < 2^64) */ inline static uint64_t tinymt64_generate_uint64(tinymt64_t * random)
the latter in header file (and inline static
), because simple, , original author wanted make clear c compilers function can inlined (and "cost" of function call should avoided).
there no function random seed (an unsigned 64-bit integer), either prng generate same sequence of random numbers, or we'll need somehow generate suitable seed ourselves.
let's implement above comments example program. let's use fixed seed, 1
, simplicity.
#include <stdlib.h> #include <inttypes.h> #include <stdio.h> #include "tinymt64.h" int main(void) { uint64_t seed = uint64_c(1); uint64_t value; tinymt64_t prng; tinymt64_init(&prng, seed); value = tinymt64_generate_uint64(&prng); printf("the first value using seed %" priu64 " %" priu64 ".\n", seed, value); return exit_success; }
because use gcc, saved above example.c
, put 2 files in same directory, , compiled them using
gcc -wall -o2 -c tinymt64.c gcc -wall -o2 -c example.c gcc -wall -o2 example.o tinymt64.o -o example
so can run using
./example
the code standard c, , should compile on systems tinymt64 compiles, including on windows.
let's @ code.
first, tinymt64_init()
takes pointer tinymt64_t
, , uint64_t
seed. first used state of generator, , second seed value (used initialize state). because state small structure (struct tinymt64_t
, later typedef'd tinymt64_t
in header file), , has no flexible array member, can declare state normal variable, tinymt64_t prng;
. pointer state &prng
. function returns nothing.
second, tinymt64_generate_uint64()
takes pointer generator state, , returns pseudorandom number uint64_t
.
finally, uint64_c()
macro defined in inttypes.h
(or stdint.h
), , allows define unsigned 64-bit integers 0
18446744073709551615
, inclusive, without guessing type suffix (none, ul
, or possibly ull
) might need on particular compiler. (if constant smaller 231 = 2147483648
, don't need macro, having no harm.)
the priu64
macro defined in inttypes.h
, , defines conversion pattern (except leading %
, optional size fields) printf()
family of functions needs print uint64_t
properly.
if wanted read seed e.g. command line, can use e.g.
int main(int argc, char *argv[]) { uint64_t seed = uint64_c(1); uint64_t value; tinymt64_t prng; char dummy; if (argc > 1) { if (sscanf(argv[1], "%" scnu64 " %c", &seed, &dummy) != 1) { fprintf(stderr, "%s: not unsigned 64-bit integer.\n", argv[1]); return exit_failure; } }
which uses first command-line argument, if 1 specified, , default constant seed (1
) otherwise.
Comments
Post a Comment