发布于 2026-01-05 12 阅读
0

Rando.js:取代 Math.random()

Rando.js:取代 Math.random()

🙉 到底发生了什么?

现在, JavaScript 内置的 `Math.random()` 函数有了更优秀的替代方案,它将大大简化你的工作。Rando.js帮助 JavaScript 开发者以更简洁、更易读、更安全的方式编写随机数生成代码。无论你需要查找两个数字之间的随机整数/浮点数、从数组中随机抽取一个值、从 jQuery 对象中随机选择一个元素、从字符串中获取一个字符、抛硬币,还是执行任何类似的操作,甚至需要防止重复,我们都能以加密级别的安全性为你提供保障。最棒的是什么?我们的库极其轻量级且对开发者友好——这意味着它不会给你的项目带来任何负担,而且实现起来也极其简单。你可以在线或在GitHub上找到它。


⚡ 快速实施

步骤 1:将以下脚本标签粘贴到 HTML 文档的 <head> 部分:

<script src="https://randojs.com/2.0.0.js"></script>
Enter fullscreen mode Exit fullscreen mode

或者,使用 npm:

//Install:
npm i @nastyox/rando.js@2.0.0

//Then, paste this at the top of your JavaScript file:
const randojs = require('@nastyox/rando.js'), rando = randojs.rando, randoSequence = randojs.randoSequence;
Enter fullscreen mode Exit fullscreen mode

步骤 2:您可以根据需要,在文档的 JavaScript 中使用https://randojs.com/中解释的任何命令。


🎉 示例

   rando()                       //a floating-point number between 0 and 1 (could be exactly 0, but never exactly 1)  
   rando(5)                      //an integer between 0 and 5 (could be 0 or 5)  
   rando(5, 10)                  //a random integer between 5 and 10 (could be 5 or 10)  
   rando(5, "float")             //a floating-point number between 0 and 5 (could be exactly 0, but never exactly 5)  
   rando(5, 10, "float")         //a floating-point number between 5 and 10 (could be exactly 5, but never exactly 10)  
   rando(true, false)            //either true or false  
   rando(["a", "b"])             //{index:..., value:...} object representing a value of the provided array OR false if array is empty  
   rando({a: 1, b: 2})           //{key:..., value:...} object representing a property of the provided object OR false if object has no properties  
   rando($("div"))               //{index:..., value:...} object representing a jQuery element from the provided jQuery element set OR false if the provided jQuery element set does not contain any elements  
   rando("Gee willikers!")       //a character from the provided string OR false if the string is empty. Reoccurring characters will naturally form a more likely return value  
   rando(null)                   //ANY invalid arguments return false  
Enter fullscreen mode Exit fullscreen mode

⇢ 通过获取序列并循环执行来防止重复

   randoSequence(5)              //an array of integers from 0 through 5 in random order  
   randoSequence(5, 10)          //an array of integers from 5 through 10 in random order  
   randoSequence(["a", "b"])     //an array of {index:..., value:...} objects representing the values of the provided array in random order  
   randoSequence({a: 1, b: 2})   //an array of {key:..., value:...} objects representing the properties of the provided object in random order  
   randoSequence($("div"))       //an array of {index:..., value:...} objects representing all jQuery elements from the provided jQuery element set in random order  
   randoSequence("Good gravy!")  //an array of the characters of the provided string in random order  
   randoSequence(null)           //ANY invalid arguments return false
Enter fullscreen mode Exit fullscreen mode


如果您觉得这个项目有用,请花点时间将网站加入书签/在GitHub上给它点个星。谢谢大家!


文章来源:https://dev.to/nastyox1/rando-js-simple-read-randomness-3dc5