Random number generation

kow__kow__ Posts: 24Member

Is there an equivalent of randi(), randf() or rand_range() in Godot's shading language? There aren't any built-in functions that I know that allow me to generate random numbers.

If there aren't any, how can I generate a pseudo-random number using TIME? Thanks.


  • MegalomaniakMegalomaniak Posts: 1,087Admin
    edited May 2018 Answer ✓

    you can create a uniform int/float that you pass your random number into from a script if there is no alternative(looking through the shading language docs right now).

    edit: yep, go the uniform route since I can't see any convenience feature implemented for this. But then one isn't really needed, so...

  • kow__kow__ Posts: 24Member

    Thanks! I think I'll go the uniform route, then.

  • sdfgeoffsdfgeoff Posts: 9Member
    edited May 2018

    This is a fairly standard function that you can find dotted around the web (in this case I pulled it from here):

    float rand(vec2 co){
        return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);

    It is useful for creating noise inside a shader (eg perlin noise). There are many other noise functions that can be created. In this video, the terrain was randomly generated inside the vertex shader using a ported version of blender's hetero_terrain generator (and some other modifications).

  • MegalomaniakMegalomaniak Posts: 1,087Admin
    edited May 2018

    I've done that back in godot 2 and it's got it's problems.

    Specifically: it's view dependent.

  • sdfgeoffsdfgeoff Posts: 9Member

    Only if you feed in the view coordinates. If you make a 1-d equivalent function, and feed in time, then it isn't view dependent.

Sign In or Register to comment.