Javascript Math.random()未声明的标识符

Javascript Math.random()未声明的标识符,javascript,html,webgl,fragment-shader,Javascript,Html,Webgl,Fragment Shader,我试图在片段着色器中生成随机颜色,但遇到了编译器错误。这是我的密码: <!--here is where the color is set--> <script id="fragment-shader" type="x-shader/x-fragment"> precision mediump float; void main() { float red = Math.random();

我试图在片段着色器中生成随机颜色,但遇到了编译器错误。这是我的密码:

    <!--here is where the color is set-->
    <script id="fragment-shader" type="x-shader/x-fragment">

    precision mediump float;

    void
    main()
    {
        float red = Math.random();
        float green = 0.5;
        float blue = 0.7;
        gl_FragColor = vec4( red, green, blue, 1.0 );
    }
    </script><!--here is where the color is set-->

精密中泵浮子;
无效的
main()
{
float red=Math.random();
浮动绿色=0.5;
浮蓝=0.7;
gl_FragColor=vec4(红色、绿色、蓝色,1.0);
}
以下是我得到的错误:

    Fragment shader failed to compile.  The error log is:<pre>ERROR: 0:8: 'Math' : undeclared identifier 
    ERROR: 0:8: '' : methods are not supported 
    ERROR: 0:8: 'random' : no matching overloaded function found 
    </pre>
片段着色器未能编译。错误日志为:错误:0:8:“数学”:未声明的标识符
错误:0:8:“”:不支持方法
错误:0:8:“随机”:未找到匹配的重载函数

我对HMTL、WebGL和Javascript都是新手,我不知道是什么问题,为什么会出现问题。为什么我不能使用Math.random?

Math是一个JavaScript对象,着色器是用JavaScript编写的

要在GLSL中生成随机数,请参见@asimes的注释。

Duplicate: