Generate random number between two numbers in Java

2018-12-31 05:16发布

Is there a way to generate a random number in a specified range (e.g. from 1 to 6: 1, 2, 3, 4, 5, or 6) in JavaScript?

18条回答
弹指情弦暗扣
2楼-- · 2018-12-31 05:39
var x = 6; // can be any number
var rand = Math.floor(Math.random()*x) + 1;
查看更多
春风洒进眼中
3楼-- · 2018-12-31 05:41

Other solutions:

  • (Math.random() * 6 | 0) + 1
  • ~~(Math.random() * 6) + 1
查看更多
柔情千种
4楼-- · 2018-12-31 05:41

In order to create a random number in javascript we can use the built in javascript Math object. The Math object holds a function Math.random(). This function does the following (source MDN):

The Math.random() function returns a floating-point, pseudo-random number in the range 0–1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range.

Given this functionality we can easily scale this into more complex scenarios. For example:

function randomNR (x, y){
return Math.round((Math.random() * x + y));
}

// dice >> random number between 1-6
// dice functionality without function >> Math.round((Math.random() * 5 + 1))
console.log(randomNR(5,1));


// coin >> random number between 0-1
// coin functionality without function >> Math.round((Math.random() * 1))
console.log(randomNR(1,0))

查看更多
梦醉为红颜
5楼-- · 2018-12-31 05:43

Math.random()

From the Mozilla Developer Network documentation:

// Returns a random integer between min (include) and max (include)

Math.floor(Math.random() * (max - min + 1)) + min;

Useful examples:

// 0 -> 10
Math.floor(Math.random() * 11);

// 1 -> 10
Math.floor(Math.random() * 10) + 1;

// 5 -> 20
Math.floor(Math.random() * 16) + 5;

// -10 -> (-2)
Math.floor(Math.random() * 9) - 10;
查看更多
残风、尘缘若梦
6楼-- · 2018-12-31 05:43

Inspite of many answers and almost same result. I would like to add my answer and explain its working. Because it is important to understand its working rather than copy pasting one line code. Generating random numbers is nothing but simple maths.

CODE:

function getR(lower, upper) {

  var percent = (Math.random() * 100);
  // this will return number between 0-99 because Math.random returns decimal number from 0-0.9929292 something like that
  //now you have a percentage, use it find out the number between your INTERVAL :upper-lower 
  var num = ((percent * (upper - lower) / 100));
  //num will now have a number that falls in your INTERVAL simple maths
  num += lower;
  //add lower to make it fall in your INTERVAL
  //but num is still in decimal
  //use Math.floor>downward to its nearest integer you won't get upper value ever
  //use Math.ceil>upward to its nearest integer upper value is possible
  //Math.round>to its nearest integer 2.4>2 2.5>3   both lower and upper value possible
  console.log(Math.floor(num), Math.ceil(num), Math.round(num));
}
查看更多
裙下三千臣
7楼-- · 2018-12-31 05:44

I was searching random number generator written in TypeScript and I have written this after reading all of the answers, hope It would work for TypeScript coders.

    Rand(min: number, max: number): number {
        return (Math.random() * (max - min + 1) | 0) + min;
    }   
查看更多
登录 后发表回答