Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

javascript - 在JavaScript中生成特定范围内的随机整数?(Generating random whole numbers in JavaScript in a specific range?)

如何可以生成两个指定的变量之间的随机整数在JavaScript中,例如x = 4y = 8将输出任何的4, 5, 6, 7, 8

  ask by zacharyliu translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are some examples on the Mozilla Developer Network page:(Mozilla开发人员网络页面上有一些示例:)

/** * Returns a random number between min (inclusive) and max (exclusive) */ function getRandomArbitrary(min, max) { return Math.random() * (max - min) + min; } /** * Returns a random integer between min (inclusive) and max (inclusive). * The value is no lower than min (or the next integer greater than min * if min isn't an integer) and no greater than max (or the next integer * lower than max if max isn't an integer). * Using Math.round() will give you a non-uniform distribution! */ function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } Here's the logic behind it.(这是背后的逻辑。) It's a simple rule of three:(这是三个简单的规则:) Math.random() returns a Number between 0 (inclusive) and 1 (exclusive).(Math.random()返回一个介于0(含)和1( Math.random()之间的Number 。) So we have an interval like this:(所以我们有一个这样的间隔:) [0 .................................... 1) Now, we'd like a number between min (inclusive) and max (exclusive):(现在,我们想要一个介于min (含)和max (不含)之间的数字:) [0 .................................... 1) [min .................................. max) We can use the Math.random to get the correspondent in the [min, max) interval.(我们可以使用Math.random在[min,max)间隔中获取对应的对象。) But, first we should factor a little bit the problem by subtracting min from the second interval:(但是,首先我们应该通过从第二个间隔中减去min来解决这个问题:) [0 .................................... 1) [min - min ............................ max - min) This gives:(这给出:) [0 .................................... 1) [0 .................................... max - min) We may now apply Math.random and then calculate the correspondent.(现在我们可以应用Math.random ,然后计算对应的对象。) Let's choose a random number:(让我们选择一个随机数:) Math.random() | [0 .................................... 1) [0 .................................... max - min) | x (what we need) So, in order to find x , we would do:(因此,为了找到x ,我们要做:) x = Math.random() * (max - min); Don't forget to add min back, so that we get a number in the [min, max) interval:(别忘了加min ,这样我们就可以在[min,max)间隔中得到一个数字:) x = Math.random() * (max - min) + min; That was the first function from MDN.(这是MDN的第一个功能。) The second one, returns an integer between min and max , both inclusive.(第二个,返回介于minmax之间(包括两个端点)的整数。) Now for getting integers, you could use round , ceil or floor .(现在要获取整数,可以使用roundceilfloor 。) You could use Math.round(Math.random() * (max - min)) + min , this however gives a non-even distribution.(您可以使用Math.round(Math.random() * (max - min)) + min ,但这会产生不均匀的分布。) Both, min and max only have approximately half the chance to roll:(minmax滚动率只有大约一半:) min...min+0.5...min+1...min+1.5 ... max-0.5....max └───┬───┘└────────┬───────┘└───── ... ─────┘└───┬──┘ ← Math.round() min min+1 max With max excluded from the interval, it has an even less chance to roll than min .(如果从间隔中排除了max ,则滚动的机会比min还要少。) With Math.floor(Math.random() * (max - min +1)) + min you have a perfectly even distribution.(使用Math.floor(Math.random() * (max - min +1)) + min您可以获得完美的均匀分布。) min.... min+1... min+2 ... max-1... max.... max+1 (is excluded from interval) | | | | | | └───┬───┘└───┬───┘└─── ... ┘└───┬───┘└───┬───┘ ← Math.floor() min min+1 max-1 max You can't use ceil() and -1 in that equation because max now had a slightly less chance to roll, but you can roll the (unwanted) min-1 result too.(您不能在该方程式中使用ceil()-1 ,因为max现在滚动的机会略少,但是您也可以滚动(不需要的) min-1结果。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...