rand() function in Compute Shader?

117 views
Skip to first unread message

Hoda Naghibijouybari

unread,
May 21, 2019, 12:55:39 PM5/21/19
to WebGL Dev List
Hello,
 

Is there any way to use rand() function or implement it in compute shader in WebGL?

Best,
Hoda

Jan Scheurer

unread,
May 21, 2019, 1:22:22 PM5/21/19
to webgl-d...@googlegroups.com
Afaik the API is not responsible for the actual implementation of the rand functions but the GPU vendors are and none of them ever did it so you're most likely left to roll your own or use any of the existing open source GLSL pseudo random implementations.

--
You received this message because you are subscribed to the Google Groups "WebGL Dev List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webgl-dev-lis...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/webgl-dev-list/dc043ac7-2575-48fe-9108-7ce1fcb1aade%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Evgeny Demidov

unread,
May 21, 2019, 3:43:55 PM5/21/19
to webgl-d...@googlegroups.com
Simple (and old :) linear congruential generator with int32 data is used in 2D Ising model at  https://www.ibiblio.org/e-notes/Perc/ising.htm (WebGL2 is used for int32 textures). But there are better modern algorithms see https://en.wikipedia.org/wiki/Xorshift. E.g. Algorithm "xor128" from p. 5 of Marsaglia, "Xorshift RNGs" (see wiki). In JavaScript it is

let s = 0, N = 1000000, N1 = 1/N, m = 1/2147483647
let state = new Uint32Array([1,2,3,4])

    for (let y = 0; y < N; y++){
      let t = xorshift128()*m
      s += t
    }
    console.log(s/N)

function xorshift128(){
 let s, t = state[3];
 state[3] = state[2];
 state[2] = state[1];
 state[1] = s = state[0];
 t ^= t << 11;
 t ^= t >> 8;
 return state[0] = t ^ s ^ (s >> 19);
}

Evgeny

--
Reply all
Reply to author
Forward
0 new messages