Jump to content

Pseudorandom Generator


Josh
 Share

Recommended Posts

Not tested much, just an experiment. Can be slightly faster (25%) than rand() or mt19937:

float random(const float x, const float y)
{
    const float m = sin(x * 12.9898f + y * 78.233f) * 43758.5453f;
    return m - floor(m);
}

 

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Just a side node: On intel integrated gfx card this glsl code is producing horrible noise. But you can overcome this issue by significantly lowering the 

 43758.5453f
On 6/4/2022 at 12:44 PM, havenphillip said:

That's cool. It didn't like "float m" as a constant for some reason idk.

I think Josh's code is a conversion to c++ code.  Yours is correct for glsl. 

 

Edited:

As a side node, this should always replaced with a noise texture lookup in glsl when possible. While it is faster in c++ than traditional random generators it will slow down glsl shaders when it is used to much. A simple texture lookup is much faster. (one way is to pregenerate a texture with this function and later use this texture to lookup the random number.

  • Like 2
  • Windows 10 Pro 64-Bit-Version
  • NVIDIA Geforce 1080 TI
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...