Is there a way to generate a random value only once and ensure that it remains constant, even when called within a function? If I use the Random method inside a function, the value of 'x' changes every time the function is called due to the method generating a new value each time.
I tried creating a global variable and assigning the result of the Random method to it. This worked in ensuring the random value remained the same. However, I need the range of the random value to be defined based on user input. Since user input needs to come before the Random method runs, this approach doesn't work.
So, what should be done in this scenario? The user should be able to define the range of the random value, while 'x' should only take on a single value.
This is important because I plan to use the 'x' variable in another method.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="jscss.css" />
</head>
<script>var size = document.getElementById("size"); var x = Math.floor(Math.random(size) * (5)) + 1; function afunc() { alert(x); }</script>
<body>
<input type="number" id="size"><br /> <button onclick="afunc()">Submit</button><br />
</body>
</html>