JS: How to exclude a range of values whilst using Math.random()?

S Craig

I'm generating some random x and y coordinates for a player in a game:

var xcoordRed = Math.floor((Math.random() * 790) +1);
var ycoordRed = Math.floor((Math.random() * 400) +1);

But I need to exclude from xcoordRed the range 325 - 375 and from ycoordRed 150 - 250.

How would I go about doing this efficiently?

Shomz

Simple math, reduce the range of the random call by the difference of the excluded range, and then if the random value is bigger than the lower exclusion delimiter, just increase it by the exclusion difference. Meaning:

var diff = 375 - 325; // add 1 if inclusive
var xcoordRed = Math.floor((Math.random() * (790 - diff)) +1);
if (xcoordRed >= 325)
    xcoordRed += diff;

// the same approach goes for Y

This means that, if the random value is, say, 324, it will remain 324. But, if it's 330, it will become 380, etc. The highest possible random number, 740, will become 790, of course.


var r = document.getElementById('r');
function gen() {
  var diffx = 375 - 325;
  var diffy = 250 - 150;
  var xcoordRed = Math.floor((Math.random() * (790 - diffx)) + 1);
  if (xcoordRed >= 325)
    xcoordRed += diffx;
  var ycoordRed = Math.floor((Math.random() * (400 - diffy)) + 1);
  if (ycoordRed >= 150)
    ycoordRed += diffy;
  
  r.textContent = 'X: ' + xcoordRed + ' Y: ' + ycoordRed;
}
<button onclick="gen()">Generate</button>
<div id="r"></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Math.random range using the size of an arraylist

From Dev

How do you generate a random number with equal probability using Math.random() in Java

From Dev

How to generate random numbers in specyfic range using pareto distribution in Python

From Dev

How to change the rate limit whilst using a RateGate?

From Dev

How to generate random numbers the give fixed results using Math.random

From Dev

Getting a number in a range using math

From Dev

Java Math.random() How random is it?

From Dev

Math.random specific values corona sdk

From Dev

How to fill an array with random values from a range? (Duplicates are OK.)

From Dev

How to specify range on math.random?

From Dev

How does Math.random() generate random numbers beyond it's "native" range?

From Dev

How to exclude a facet range field using SOLR?

From Dev

How do you create a random range, but exclude a specific number?

From Dev

Lua math.random returns erratic values

From Dev

Random number range, exclude 0

From Dev

How to place value at random locations in an array using Math.random?

From Dev

Node.js - How to generate random numbers in specific range using crypto.randomBytes

From Dev

Is generating and concatenating 3 Math.random() values more random than 1 Math.random() value?

From Dev

How do you generate a random number with equal probability using Math.random() in Java

From Dev

Math.random specific values corona sdk

From Dev

How do you search for a range of number using Secure Random?

From Dev

How to get Math.random() to add to returned values rather than replacing them

From Dev

generate a random number within a range but exclude some in C/C++

From Dev

JS: How to exclude a range of values whilst using Math.random()?

From Dev

Is generating and concatenating 3 Math.random() values more random than 1 Math.random() value?

From Dev

How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

From Dev

How to display a specific range of random numbers in java using Math.random

From Dev

SQL - How to exclude weekends from date range using 'datediff'

From Dev

How to use math.random again to get a different value if the values are the same in javascript

Related Related

  1. 1

    Math.random range using the size of an arraylist

  2. 2

    How do you generate a random number with equal probability using Math.random() in Java

  3. 3

    How to generate random numbers in specyfic range using pareto distribution in Python

  4. 4

    How to change the rate limit whilst using a RateGate?

  5. 5

    How to generate random numbers the give fixed results using Math.random

  6. 6

    Getting a number in a range using math

  7. 7

    Java Math.random() How random is it?

  8. 8

    Math.random specific values corona sdk

  9. 9

    How to fill an array with random values from a range? (Duplicates are OK.)

  10. 10

    How to specify range on math.random?

  11. 11

    How does Math.random() generate random numbers beyond it's "native" range?

  12. 12

    How to exclude a facet range field using SOLR?

  13. 13

    How do you create a random range, but exclude a specific number?

  14. 14

    Lua math.random returns erratic values

  15. 15

    Random number range, exclude 0

  16. 16

    How to place value at random locations in an array using Math.random?

  17. 17

    Node.js - How to generate random numbers in specific range using crypto.randomBytes

  18. 18

    Is generating and concatenating 3 Math.random() values more random than 1 Math.random() value?

  19. 19

    How do you generate a random number with equal probability using Math.random() in Java

  20. 20

    Math.random specific values corona sdk

  21. 21

    How do you search for a range of number using Secure Random?

  22. 22

    How to get Math.random() to add to returned values rather than replacing them

  23. 23

    generate a random number within a range but exclude some in C/C++

  24. 24

    JS: How to exclude a range of values whilst using Math.random()?

  25. 25

    Is generating and concatenating 3 Math.random() values more random than 1 Math.random() value?

  26. 26

    How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

  27. 27

    How to display a specific range of random numbers in java using Math.random

  28. 28

    SQL - How to exclude weekends from date range using 'datediff'

  29. 29

    How to use math.random again to get a different value if the values are the same in javascript

HotTag

Archive