»
EnglishFrenchVietnamese

Print - Random Number Generator - JavaScriptBank.com

Full version: jsB@nk » Calculation » Random Number Generator
URL: https://www.javascriptbank.com/random-number-generator.html

Random Number Generator © JavaScriptBank.comThe function randInt(min, max) can be easily adapted for any application where you need to select a random integer within a certain range (i.e. from 1 to 100). The rest of the code is an example of how it can be used.

Full version: jsB@nk » Calculation » Random Number Generator
URL: https://www.javascriptbank.com/random-number-generator.html



JavaScript
<script language="Javascript">/*USAGEDo function call RandInt(mininum value, maximum value)For example, to find a random number between 1 and 12 userandInt(1,12)*/function randInt(min, max){var div = (max - min) + 1var randNum = Math.random()for (var i = 0; i <= div - 1; i++){if (randNum >= i / div && randNum < (i+1) / div){return i + min}}}function getNums(){var min = parseInt(document.myForm.min.value)var max = parseInt(document.myForm.max.value)var runs = parseInt(document.myForm.runs.value)for(var x = 1;x <= runs;x++){document.myForm.nums.value += "" + randInt(min,max) + "\n"}}</script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<form name="myForm" style="text-align: left; width: 50%;"><p><font face="Arial">Min: <input type="text" name="min" size="2"><br>Max:<input type="text" name="max" size="2"><br>Runs: <input type="text" name="runs" size="2"> (How many numbers would you like to produce?)</font></p><p><font face="Arial"><textarea rows="12" name="nums" cols="29"></textarea></font></p><p><font face="Arial"><input type="button" value="Go!" name="go" onclick="getNums()"></font></p></form><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->