如何在Javascript中的两个指定变量之间生成随机整数,例如x = 4
并y = 8
输出4,5,6,7,8中的任何一个?
我正在尝试使用JavaScript编写的脚本创建唯一标识符.我应用于这些标识符的约束如下:
我知道任何编程语言都不能表示真正的随机性.有些编程语言只是有一个库函数模拟"随机性",但我不认为任何语言实际上,实现了真正的随机性的想法.不过,让我们假装JavaScript Math.random()函数是随机性的最佳实现.
这是我到目前为止的JavaScript代码:
// the numbers in this array represent the utf-8 codes for the uppercase letters A-Z
var uppercaseLetters = new Array(65,66,67,68,69,70,71,72,73,74,75,76,77,78,
79,80,81,82,83,84,85,86,87,88,89,90);
// http://stackoverflow.com/questions/16753876/javascript-button-to-pick-random-item-from-array
var randomElement = uppercaseLetters[Math.floor(Math.random() *
uppercaseLetters.length)];
document.write(String.fromCharCode(randomElement));
convertedElement = String.fromCharCode(randomElement);
// document.write("<br /> <br />");
// borrowed String.fromCharCode from some part of the Mozilla developer website
// you can also see the utf-8 character set on wikipedia:
// http://en.wikipedia.org/wiki/UTF-8
/* for(var index = 0; index < 26; index++) { …
Run Code Online (Sandbox Code Playgroud)