wamu hash

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

wamu hash

Post by dafhi »

mostly a procedural graphics RNG brought to you by a graphics person

updated with basic run-time text

Code: Select all

/' -- wamu hash - 2021 Aug 31, by dafhi
 
  keyboard: Q, W, arrows

  Wamu is a hash-based RNG, shorthand for Warmup Multiplexer.  My brain posted the name ;)
   
    algo highlights:
   
  1. stable period
  2. many even multipliers produce good results
  3. developed for procedural graphics
    
    
    [ updates ]
    
  Aug 31
   ---
  - added
  intro loop
    
  Aug 30
   ---
  - added
  run-time text
  - renamed
  mul0, add0 -> mulA, addA
  - removed
  min(), max(), ROTL() [unused defines], small_mask, bucket test and algo name info
 
'/


type    statelit      as ulong  '' generator size


/'  hash util
'/
const   lenx8 = len(statelit) * 8


/'  util
'/
#define def           #define


/'  less typing
'/
#undef  int
def     int           As integer
def     sng           as single

def     flo(x)        (((x)*2.0-0.5)shr 1)      '' replaces int() - http://www.freebasic.net/forum/viewtopic.php?p=118633


''
  namespace myhash
 
dim as statelit   a,b,c,d,e,f         '' states

dim as statelit   mulA = 1
dim as statelit   addA = 1

/' output function -- populate "a" quickly given likelihood of small input "i"
'/
function warmup(i as ulongint = 0) as statelit
  const m = lenx8 / 2 - 1
 
  f = mulA * (i + addA)   '' future iterative stream RNG will use states (a d e f)
 
  f xor= (i shr 1)        '' stabilizes period for some reason
 
  e = f + f shr (f mod m)
  d = e xor (f shr 1)
  a = d + d shl m
 
  return a
end function

  End Namespace



def     rng           myhash.warmup  '' non-float

function bpp_col(i as statelit = 0) as ulongint
  dim as ubyte c
  def q rng 
  select case as const lenx8
  case 8
    return q(i)*(1+256+65536)
  case 16
    return (q(i) shl 8) xor q(i+0)
  case 32
    return q(i)
  case 64
    return q(i)
  End Select
End Function


const         COLOR_ON = rgb(165,195,235)
const         COLOR_OFF  = rgb(40,110,210)

sub PixelBits( x as short, y as short, i as statelit = 0, pix_size as statelit = 4)
  var lenm = pix_size - 1
  var valu = bpp_col(i)
  for shift as long = lenx8-1 to 0 step -1
    dim as ulong col = IIF( (valu shr shift)and 1, COLOR_ON, COLOR_OFF )
    line (x,y)-(x+lenm, y+lenm),col, bf
    x += pix_size
  Next
End Sub


const           w = 256 * 3, _
                h = 256 * 2

sub raw_pattern( pel_size int = 1, cols int = 0, rows int = 0, bit_level as boolean = false)
 
  var magnify = cols or rows
  var _x = iif( cols = 0, 0, 2)
  var _y = iif( rows = 0, 0, 2)
  var stepx = lenx8*pel_size
 
  if magnify or bit_level then
    line (_x-1, _y-1) - (_x + cols * stepx, _y + rows * pel_size), rgb(0,0,0), bf
    if cols=0 then cols = w \ stepx
    if rows=0 then rows = h \ pel_size
    dim as statelit i
    for x int = _x to _x + (cols-1) * stepx step stepx
      for y int = _y to _y + (rows-1) * pel_size step pel_size
        PixelBits x, y, i, pel_size
        i += 1
      next
    next
 
  else
    for y int = _y to h - 1
      for x int = _x to w - 1
        var i = (bpp_col((x-_x)*h+(y-_y)) and &HFFFFFF)
        pset (x, y), i
      Next
    Next
 
  EndIf
end sub


function variable_name( p as any ptr) as string
    select case p
  case @myhash.addA
    return "addA"
  case @myhash.mulA
    return "mulA"
  End Select
End Function


sub graphics_page(byref p0 as statelit ptr)
  screenlock
    var bit_level = false
    raw_pattern 1, 0,0, bit_level
   
    var cols        = 1
    var rows        = 64
    var pixel_size  = 6
    raw_pattern pixel_size, cols, rows
    
    locate 51,1
   
    ? "adjusting "; variable_name(p0);": "; *p0
  screenunlock
End Sub


sub graphics_intro(byref p0 as statelit ptr)
  var mulA = 1f
  dim int sleepval = 100
  
  for i int = 1 to 9
    myhash.mulA = mulA
    graphics_page p0
    
    mulA *= 3
    
    sleep sleepval
    sleepval *= .6
  Next
End Sub

  
  sub Main

screenres w, h, 32

var intro_message_key_tick = 60
var p0 = @myhash.mulA

graphics_intro p0

dim as long   _k
do
 
  graphics_page p0
  if intro_message_key_tick then
    locate 46,1
    ? " keys: Q, W, arrows"
    intro_message_key_tick -= 1
  endif
  
  _k = getkey
  dim as string k = chr(_k and 255)
 
  if k=chr(27) then exit do
  if k = " " then exit do
   
    select case ucase(k)
  case "Q"
    p0 = @myhash.mulA
  case "W"
    p0 = @myhash.addA
  End Select
 
  k = chr(_k shr 8)
 
  '' up down
  if k[0] = 72 then *p0 *= 1.75
  if k[0] = 80 then *p0 *= .67

  '' left right
  if k[0] = 75 then *p0 -= 1
  if k[0] = 77 then *p0 += 1
loop

End sub


Main
Last edited by dafhi on Aug 31, 2021 23:07, edited 3 times in total.
deltarho[1859]
Posts: 4305
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: wamu hash

Post by deltarho[1859] »

@dafhi

You have gone to a lot of trouble developing 'wamu hash' but not got a single response.

With such a title, my first thought was a new hash function. It turns out to be a PRNG. So, its name needs changing.

You mention 'stable period' but do not tell us what that is.

There is no mention of the quality of distribution uniformity or randomness, which can be examined with John Walker's ENT or Chris Doty-Humphrey's PractRand.

No help is given in regard to usage in our applications by providing a Usage Example.

The 2D outputs have stripes, indicating non-randomness.

If you come up with a new PRNG you have to 'sell it' and that is not been done here.

Just posting a piece of code without some of the above, if not all, will see no one touching it with a barge pole.

My PRNG posts don't get many responses and, to some extent, that may be because of a lack of interest. However, I get many views, which tells me that my 'ramblings' don't leave much left for questions.

So let's see a post #2 with some analysis and see how many views you then get and, hopefully, some responses.

I hope this post does not knock you back - it is meant as a friendly critique. I also think it courageous to post a new PRNG so well done on that score.
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: wamu hash

Post by dafhi »

keys: Q, W, arrows do stuff.

- 2 hash types to my knowledge
1. fill all values within a range (given enough inputs
2. prng

by stable period i mean it is always 2^(state bits)

- an example

code start

Code: Select all

type    statelit      as ushort  '' generator size
function warmup(i as ulongint = 0) as statelit

Code: Select all

  #if 1
    a = mul0 * i + add0
    a xor= a shr 2
    a *= mul0
  #else
    f = mul0 * (i + add0)
   
    f xor= (i shr 1)        '' stabilizes period for some reason
   
    e = f + f shr (f mod mo)
    d = e xor (f shr 1)
    a = d + d shl mo         '' future iterative stream RNG will use states (a d e f)
  #EndIf
adjust multiplier with arrow keys
deltarho[1859]
Posts: 4305
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: wamu hash

Post by deltarho[1859] »

@dafhi

Your last post was subject to a few edits. I am pleased to see that you had second thoughts about one comment and deleted it. Image

Added: The add0 never look good, but the mul0 12 and above do look good.
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: wamu hash

Post by dafhi »

i never know what to call variables half the time. might rename to addC
deltarho[1859]
Posts: 4305
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: wamu hash

Post by deltarho[1859] »

Perhaps a user error?

On opening, I get 'mul0: 1' with stripes, which is, obviously, bad news. If I switch to 'add0: 1', via "W", the stripes are still there and remain when pressing the right arrow key quite a few times.

On the other hand, on opening if I press the right arrow key to get me to 'mul0: 12', say, then switch to 'add0: 1' there are no stripes and the randomness is good from the start.

So, is it me or does your code need an edit somewhere?
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: wamu hash

Post by dafhi »

[edit] - not a bug. i wanted to show how algorithm behaves with different conditions

a person told me that if i can get a demo to show as much as possible without user interaction, that is a win. I'll try to think of something without adding too much code .. [done]
Post Reply