当前位置:网站首页>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)}`)
边栏推荐
猜你喜欢

Adobe Premiere基础-常用的视频特效(裁剪,黑白,剪辑速度,镜像,镜头光晕)(十五)
![[embedded foundation] serial port communication](/img/b7/18fec20e2d5fa5f226c6f8bb1e93d2.png)
[embedded foundation] serial port communication

Implementation of timed tasks in laravel framework

什么是数字化?什么是数字化转型?为什么企业选择数字化转型?

1382. 将二叉搜索树变平衡-常规方法

How fiddle uses agents

Data analysts too hot? Monthly income 3W? Tell you the true situation of this industry with data

【牛客讨论区】第四章:Redis

Review of drug discovery-03-molecular design and optimization

Raspberry pie realizes intelligent cooling by temperature control fan
随机推荐
Raspberry pie realizes intelligent cooling by temperature control fan
面试官问:JS的this指向
Ten thousand words long article understanding business intelligence (BI) | recommended collection
Coscon'22 lecturer solicitation order
[Yocto RM]1 - System Requirements
Adobe Premiere foundation - sound adjustment (volume correction, noise reduction, telephone tone, pitch shifter, parameter equalizer) (XVIII)
Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known
Scala 基础 (三):运算符和流程控制
[Yocto RM] 2 - Yocto Project Terms
[Yocto RM]3 - Yocto Project Releases and the Stable Release Process
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
JS 随机数(随机数 小数)
TIA botu_ Concrete method of making analog input and output Global Library Based on SCL language
有监督、无监督与半监督学习
如何高效读书学习
Web3 technology initial experience and related learning materials
外盘期货哪里可以开户?哪个平台出入金比较安全?
[Niuke discussion area] Chapter 4: redis
Review of drug discovery-02-prediction of molecular properties
205. 同构字符串