当前位置:网站首页>JS random number (random number decimal)
JS random number (random number decimal)
2022-06-28 02:09:00 【The most ferocious little seal】
I read some other articles on the Internet before , No direct access found [n,m] Two interval random values that are both decimals , So I wrote a
explain :
minandmaxYou can enter integers or decimals .decimal: When no input or input is zero , The maximum number of decimal places isminandmaxThe fractional partThe largest number. for example :randomNum(0, 6.88, 0)The maximum value of the decimal part is99, for example2.99, If you enter , Will be based onminandmaxalsodecimalThree to determine the maximum number of digits .- If you want random integer values ,
minandmaxInput integer ,decimalNo input or zero is OK , for example :randomNum(1, 9, 0)perhapsrandomNum(1, 9)
function randomNum(min = 0, max = 0, decimal=0) {
let min_z = Math.trunc(min); // The integer part of the minimum
let max_z = Math.trunc(max); // The integer part of the maximum
// Determine whether there is a decimal part , If there is no such thing as 0
let min_x = isNaN(getDecimalNum(min)) ? 0 : getDecimalNum(min); // The fractional part of the minimum
let max_x = isNaN(getDecimalNum(max)) ? 0 : getDecimalNum(max); // The fractional part of the maximum
// Distinguish between cases with and without decimals
if (min_x > 0 || max_x > 0 || decimal > 0) {
// Integer partial random number
let z = parseInt(Math.random() * (max_z - min_z + 1) + min_z, 10);
// Fractional random number
let x = 0;
// The maximum number of decimal random numbers
let max_decimal = min_x.toString().length > max_x.toString().length ? min_x.toString().length : max_x.toString().length;
max_decimal = decimal > max_decimal ? decimal : max_decimal;
// Determine the random integer part , Whether it is equal to the minimum or maximum value
if(z == min_z || z == max_z){
if(z == min_z){
// The integer partial random number is equal to the minimum value , Then you should start with the decimal part of the minimum value , To the maximum number of decimal places can be random
x = parseInt(Math.random() * (Math.pow(10, max_decimal) - min_x) + min_x, 10);
}else{
// The integer partial random number is equal to the maximum value , Then we should 0 Start , To the decimal part of the maximum
x = parseInt(Math.random() * (max_x + 1), 10);
}
}else{
// The integer part is in the range of maximum and minimum values , From 0 To the maximum number of decimal places can be random
x = parseInt(Math.random() * (Math.pow(10, max_decimal)), 10);
}
return Number(`${
z}.${
x}`);
} else {
return parseInt(Math.random() * (max_z - min_z + 1) + min_z, 10);
}
}
// Get the decimal part of the value
function getDecimalNum(data){
return Number(data.toString().split('.')[1]);
}
// Use
console.log(` Random number is :${
randomNum(0, 6.8, 2)}`)
边栏推荐
- 声网 VQA:将实时互动中未知的视频画质用户主观体验变可知
- Interviewer asked: Inheritance of JS
- Ten MySQL locks, one article will give you full analysis
- 外盘期货哪里可以开户?哪个平台出入金比较安全?
- 【ELT.ZIP】OpenHarmony啃论文俱乐部—数据密集型应用内存压缩
- 1382. 将二叉搜索树变平衡-常规方法
- [Yocto RM] 2 - Yocto Project Terms
- centos8-操作记录-命令版-yum-redis-mysql-nacos-jdk
- 【嵌入式基础】串口通信
- style中的scoped属性和lang属性
猜你喜欢
![[embedded foundation] memory (cache, ram, ROM, flash)](/img/bd/d952ebd4033a993dd16cd792bb3e1e.png)
[embedded foundation] memory (cache, ram, ROM, flash)
![[embedded foundation] serial port communication](/img/b7/18fec20e2d5fa5f226c6f8bb1e93d2.png)
[embedded foundation] serial port communication

Evaluation - rank sum ratio comprehensive evaluation

Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)

什麼是數字化?什麼是數字化轉型?為什麼企業選擇數字化轉型?

Cesium 点击绘制多边形(动态绘制多边形)

Machine learning notes - time series as features

Intranet penetration with FRP

Adobe Premiere Basics - general operations for editing material files (offline files, replacing materials, material labels and grouping, material enabling, convenient adjustment of opacity, project pa

fiddle如何使用代理
随机推荐
[Yocto RM]9 - QA Error and Warning Messages
有监督、无监督与半监督学习
JS 数组随机取值(随机数组取值)
外盘期货哪里可以开户?哪个平台出入金比较安全?
Numpy----np.tile()函数解析
面试官问:JS的this指向
Cesium Color 颜色(赋值)随机颜色
TIA botu_ Concrete method of making analog input and output Global Library Based on SCL language
9. Openfeign service interface call
Import the data table in MySQL into Excel
General process after reference layer reboot
The research group of Xuyong and duanwenhui of Tsinghua University has developed an efficient and accurate first principles electronic structure deep learning method and program
Prometeus 2.35.0 new features
如何阅读一篇论文
Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)
Interviewer asked: Inheritance of JS
什么是数字化?什么是数字化转型?为什么企业选择数字化转型?
【嵌入式基础】串口通信
Drug interaction prediction based on learning size adaptive molecular substructure
TI AM3352/54/59 工业核心板硬件说明书