Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
77
rated 0 times [  79] [ 2]  / answers: 1 / hits: 36209  / 13 Years ago, thu, february 16, 2012, 12:00:00

I am using



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


but it returns ALL numbers(random) between -13 and 13. how can i get it to generate a random number between -13 to -4 exluding -3 , -2, -1, 0 , 1 ,2 ,3 and including 4 to 13.


More From » jquery

 Answers
15

Get a random number between 1-10 and add 3, to get one between 4-13:



random = Math.ceil(Math.random() * 10) + 3;


Generate a random between 0-1. If it's 0, make the number negative:



random = (Math.floor(Math.random() * 2) == 0) ? 0 - random : random;


JSFiddle showing working copy:



http://jsfiddle.net/adamh/cyGwf/


[#87423] Wednesday, February 15, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
frederickmohamedw

Total Points: 21
Total Questions: 123
Total Answers: 105

Location: The Bahamas
Member since Tue, Apr 27, 2021
3 Years ago
frederickmohamedw questions
Wed, Sep 23, 20, 00:00, 4 Years ago
Sat, Jul 18, 20, 00:00, 4 Years ago
Sun, Apr 26, 20, 00:00, 4 Years ago
Sat, Jan 11, 20, 00:00, 4 Years ago
Fri, Dec 27, 19, 00:00, 4 Years ago
;