random123

Counter-based Random Number Generators
git clone git://git.meso-star.com/random123.git
Log | Files | Refs | README | LICENSE

simple.c (2682B)


      1 /*
      2 Copyright 2010-2011, D. E. Shaw Research.
      3 All rights reserved.
      4 
      5 Redistribution and use in source and binary forms, with or without
      6 modification, are permitted provided that the following conditions are
      7 met:
      8 
      9 * Redistributions of source code must retain the above copyright
     10   notice, this list of conditions, and the following disclaimer.
     11 
     12 * Redistributions in binary form must reproduce the above copyright
     13   notice, this list of conditions, and the following disclaimer in the
     14   documentation and/or other materials provided with the distribution.
     15 
     16 * Neither the name of D. E. Shaw Research nor the names of its
     17   contributors may be used to endorse or promote products derived from
     18   this software without specific prior written permission.
     19 
     20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31 */
     32 #include <Random123/threefry.h>
     33 #include <stdio.h>
     34 #include "example_seeds.h"
     35 
     36 int main(int argc, char **argv){
     37     int i;
     38     uint32_t seed = example_seed_u32(EXAMPLE_SEED1_U32); // example of user-settable seed
     39     
     40     /* while this example starts the counter from 0 and then increments by 0,
     41        this could start anywhere and increment by any stride or pattern that
     42        makes sense for the application as long as it produces a non-repeating stream */
     43     threefry4x32_ctr_t  ctr = {{0,0,0,0}};
     44     /* we illustrate one user-specified seed and one constant as the key */
     45     threefry4x32_key_t key = {{seed, EXAMPLE_SEED2_U32,0,0}};
     46     (void)argc; (void)argv; /* unused */
     47     printf( "The first few randoms with key 0x%llx 0x%llx\n",
     48 	   (unsigned long long)key.v[0], (unsigned long long)key.v[1]);
     49     for(i=0; i<10; ++i){
     50         ctr.v[0] = i;
     51 	{
     52           threefry4x32_ctr_t rand = threefry4x32(ctr, key);
     53           printf("ctr: %llx %llx threefry4x32(20, ctr, key): %llx %llx\n",
     54                  (unsigned long long)ctr.v[0], (unsigned long long)ctr.v[1],
     55                  (unsigned long long)rand.v[0], (unsigned long long)rand.v[1]);
     56 	}
     57     }
     58     return 0;
     59 }