1 //------------------------------------------------------------------------------ 2 // GraphBLAS/Demo/Source/simple_rand.c: a very simple random number generator 3 //------------------------------------------------------------------------------ 4 5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved. 6 // SPDX-License-Identifier: Apache-2.0 7 8 //------------------------------------------------------------------------------ 9 10 // The POSIX.1-2001 example of rand, duplicated here so that the same sequence 11 // will be generated on different machines. The purpose is not to generate 12 // high-quality random numbers, but to ensure the same sequence is generated 13 // on any computer or operating system. This allows the GraphBLAS tests and 14 // demos to be repeatable. 15 16 // Since the simple_rand ( ) function is replicated from the POSIX.1-2001 17 // standard, no copyright claim is intended for this specific file. The 18 // copyright statement above applies to all of SuiteSparse:GraphBLAS, not 19 // this file. 20 21 #define GB_LIBRARY 22 #include "simple_rand.h" 23 24 // simple_rand is not thread-safe 25 uint64_t simple_rand_next = 1 ; 26 27 #define SIMPLE_RAND_MAX 32767 28 29 // return a random number between 0 and SIMPLE_RAND_MAX 30 GB_PUBLIC simple_rand(void)31uint64_t simple_rand (void) 32 { 33 simple_rand_next = simple_rand_next * 1103515245 + 12345 ; 34 return ((simple_rand_next/65536) % (SIMPLE_RAND_MAX + 1)) ; 35 } 36 37 // set the seed 38 GB_PUBLIC simple_rand_seed(uint64_t seed)39void simple_rand_seed (uint64_t seed) 40 { 41 simple_rand_next = seed ; 42 } 43 44 // get the seed 45 GB_PUBLIC simple_rand_getseed(void)46uint64_t simple_rand_getseed (void) 47 { 48 return (simple_rand_next) ; 49 } 50 51 // return a random uint64_t 52 GB_PUBLIC simple_rand_i()53uint64_t simple_rand_i ( ) 54 { 55 uint64_t i = 0 ; 56 for (int k = 0 ; k < 5 ; k++) 57 { 58 i = SIMPLE_RAND_MAX * i + simple_rand ( ) ; 59 } 60 return (i) ; 61 } 62 63 // return a random double between 0 and 1, inclusive 64 GB_PUBLIC simple_rand_x()65double simple_rand_x ( ) 66 { 67 return (((double) simple_rand_i ( )) / ((double) UINT64_MAX)) ; 68 } 69 70