Ignore:
Timestamp:
Jan 12, 2022, 6:34:58 PM (3 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
Children:
00f5fde
Parents:
a10f6b4
Message:

third attempt at specialized PRNG

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    ra10f6b4 r1959528  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 10 17:03:18 2022
    13 // Update Count     : 619
     12// Last Modified On : Wed Jan 12 09:09:48 2022
     13// Update Count     : 620
    1414//
    1515
     
    386386//---------------------------------------
    387387
     388// Sequential Pseudo Random-Number Generator : generate repeatable sequence of values that appear random.
     389//
     390// Declaration :
     391//   PRNG sprng = { 1009 } - set starting seed versus random seed
     392//   
     393// Interface :
     394//   set_seed( sprng, 1009 ) - set starting seed for ALL kernel threads versus random seed
     395//   get_seed( sprng ) - read seed
     396//   prng( sprng ) - generate random value in range [0,UINT_MAX]
     397//   prng( sprng, u ) - generate random value in range [0,u)
     398//   prng( sprng, l, u ) - generate random value in range [l,u]
     399//   calls( sprng ) - number of generated random value so far
     400//
     401// Examples : generate random number between 5-21
     402//   prng( sprng ) % 17 + 5;    values 0-16 + 5 = 5-21
     403//   prng( sprng, 16 + 1 ) + 5;
     404//   prng( sprng, 5, 21 );
     405//   calls( sprng );
     406
    388407struct PRNG {
    389408        uint32_t callcnt;                                                                       // call count
     
    392411}; // PRNG
    393412
     413void set_seed( PRNG & prng, uint32_t seed_ );
    394414uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
    395415static inline {
    396         void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; } // set seed
    397416        void ?{}( PRNG & prng ) { set_seed( prng, rdtscl() ); } // random seed
    398417        void ?{}( PRNG & prng, uint32_t seed ) { set_seed( prng, seed ); } // fixed seed
     
    403422} // distribution
    404423
     424// Concurrent Pseudo Random-Number Generator : generate repeatable sequence of values that appear random.
     425//
     426// Interface :
     427//   set_seed( 1009 ) - fixed seed for all kernel threads versus random seed
     428//   get_seed() - read seed
     429//   prng() - generate random value in range [0,UINT_MAX]
     430//   prng( u ) - generate random value in range [0,u)
     431//   prng( l, u ) - generate random value in range [l,u]
     432//
     433// Examples : generate random number between 5-21
     434//   prng() % 17 + 5;   values 0-16 + 5 = 5-21
     435//   prng( 16 + 1 ) + 5;
     436//   prng( 5, 21 );
     437
    405438void set_seed( uint32_t seed_ ) OPTIONAL_THREAD;
    406 uint32_t get_seed() __attribute__(( warn_unused_result ));
     439uint32_t get_seed() __attribute__(( warn_unused_result )) OPTIONAL_THREAD;
    407440uint32_t prng( void ) __attribute__(( warn_unused_result )) OPTIONAL_THREAD; // [0,UINT_MAX]
    408441static inline {
Note: See TracChangeset for help on using the changeset viewer.