my next RNG .. CSG 3

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
dafhi
Posts: 1238
Joined: Jun 04, 2005 9:51

my next RNG .. CSG 3

Postby dafhi » Nov 02, 2018 2:33

Code: Select all

' Complex Sequence Generator 3 by dafhi - 2018 Nov 13
' high period, high configurability PRNG

' breezes PractRand to 32TB

type CSG3_LITERAL          as ubyte 'ulongint

dim shared as CSG3_LITERAL csg3_mul = 255 ' non-ideal  (32TB:  1645)
dim shared as CSG3_LITERAL csg3_add = 1   ' 2^bits co-prime?  (&H5851F42D4C957F2D)

dim shared as CSG3_LITERAL csg3_x   = any ' seeds
dim shared as CSG3_LITERAL csg3_a   = any '

const as integer shift_amount = ( len(csg3_x)*8 ) * (37 / 64) 'Recommended [max:38 min:8] / 64

function CSG3 as double
   
   csg3_a += csg3_add
   csg3_x *= csg3_mul
   csg3_x xor= csg3_x shr shift_amount
   csg3_x xor= csg3_a                  'or Add
   
   #if 0                               'option
    const as ulongint seed3 = any
    csg3_a -= (csg3_x = Seed3)
   #endif
   
   return csg3_x / 2 ^ (len(csg3_x)*8)
 
  /'  development history
   
    v1.  state = mul * (state shr j) + add
   
    v2.  a += c - (state = seed)
         state ror r
         state = mul * state + add
         state xor= a   '/

end function



' ---------

sub Main
 
 #define looper_2d  for y as integer = 0 to h-1: for x as integer = 0 to w-1

 const im_scalar   = 3

 const w = im_scalar * 210 '' 2 * 3 * 5 * 7
 const h = im_scalar * 143 '' 11 * 13

 screenres w,h,32

 const pel_size = 1

 #undef rnd
 #define rnd csg3

 for i as long = 0 to 9
  csg3_a = i
  csg3_x = 0
 
  looper_2d
   dim as long c = rnd * 256 - .5
   pset (x,y), rgb(c,c,c)
  next:  next
 
  if inkey=chr(27) then exit for
  sleep 1000
 next

 sleep

end sub

main

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest