1

I want to use Javascript to select of random numbers between a certain range:

I want it to choose:

  • 25% of random numbers between 10 to 20
  • 25% of random numbers between 100 to 150
  • 25% of random numbers between 300 to 400
  • 25% of random numbers between 450 to 475

So my results may be:

  • 5
  • 125
  • 375
  • 475

IE a random integer from each range.

Currently, my code just takes a random number from the overall range of numbers (ie random number between 1-1500 for my example above). I've simplified numbers for my example. Not sure if its relevent but it is cloudcode in Parse.com

Here's a snippet of my JS. Let me know if you need more:

Array.prototype.randomRange = function(n, ranges) {
    var a = [];
    for(var i = 0; i < n; i++) {
        var range = getRandomInt1(0, ranges.length-1);
        var r = ranges[range];
        a.push(getRandomInt1(r.min, r.max));
    }
    return a;
};

Parse.Cloud.define("firstTimeQuery", function(request, response)  {

var maxIndex = 91811;
var maxWomenIndex = 135354;
var maxMenIndex = 105357;
var arrayOfRandomNumbers;
var gender = request.params.gender;
var query = new Parse.Query("garmentsAPI");

function getRandomInt1(min, max) {
return Math.floor(Math.random() * (max - min+1)) + min;
}

    if (gender === 'Female' || gender === 'women') {
        arrayOfRandomNumbers = [].randomRange(25, [{min:10,max:20},{min:100,max:150},{min:300,max:400},{min:450,max:475}]);

        query.containedIn("womenIndex", arrayOfRandomNumbers);
        query.containedIn('gender', ['Female', 'women']);
        query.notEqualTo("viewers", request.params.user);

        console.log('female');
Ycon
  • 1,830
  • 3
  • 27
  • 56
  • 3
    See [Generating random numbers in Javascript in a specific range?](http://stackoverflow.com/questions/1527803/generating-random-numbers-in-javascript-in-a-specific-range). – Ryan Miller Sep 17 '15 at 01:32
  • So I could replace line `arrayOfRandomNumbers = [].random(35, maxWomenIndex); ` with this line `arrayOfRandomNumbers = [].random(100 - 1 + 1)) + minimum; (150 - 250 + 1)) + minimum;`? – Ycon Sep 17 '15 at 01:36

2 Answers2

2

Would this not work?

var n1 = Math.random()
if (n1 < 0.25) {
    return parseInt(Math.random()*99+1)
} else
if (n1 < 0.5) {
    return parseInt(Math.random()*100+150)
} else
if (n1 < 0.75) {
    return parseInt(Math.random()*50+1350)
} else {
    return parseInt(Math.random()*50+1450)
}       
Soren
  • 14,402
  • 4
  • 41
  • 67
  • I'm very new to JS- how would I integrate this in my existing code? Would I replace `arrayOfRandomNumbers = [].random(35, maxWomenIndex); ` with `arrayOfRandomNumbers = [].n1(35, maxWomenIndex); var n1 = Math.random() etc.` – Ycon Sep 17 '15 at 02:01
1

I think you need to refactor randomRange() to remove the randomness of its range distribution.

Array.prototype.randomRange = function(n, ranges) {
var a = [];
for(var i = 0; i < n; i++) {
    var range = getRandomInt1(0, ranges.length-1);
    var r = ranges[range];
    a.push(getRandomInt1(r.min, r.max));
}
return a;

};

var range(index) is random, that is why you are not getting an equal 25% distribution in your array_of_random_numbers. You can remove this range(index) and just do a array_of_random_numbers[index % 4]. Maybe like this:

    for(var i = 0; i < n; i++) {
        var r = ranges[i % 4];
        a.push(getRandomInt1(r.min, r.max));
    }

UPDATE: If you want uneven percentages then something like this might work:

for(var i = 0; i < ranges.length; i++) {
    for(var j = 0; j < ranges[i].percent; j++) {
        a.push(getRandomInt1(ranges[i].min, ranges[i].max));
    }
}

...

randomRange(
[{min:10,max:20,percent:parseInt(.1*10)}, // 10% of 10
 {min:100,max:150,percent:parseInt(.4*10)}, // 40%
 {min:300,max:400,percent:parseInt(.3*10)}, // 30%
 {min:450,max:475,percent:parseInt(.2*10)} // 20%
]

http://jsfiddle.net/wokokonono/jjz977um/10/

erickb
  • 6,193
  • 4
  • 24
  • 19
  • So I should use just: `Array.prototype.randomRange = function(n, ranges) { var a = []; for(var i = 0; i < n; i++) { var r = ranges[i%4]; a.push(getRandomInt1(r.min, r.max)); }` then use `[].randomRange(25, [{min:10,max:20},{min:100,max:150},{min:300,max:400},{min:450,max:475}]);`? Or do I need to use a combination of both? – Ycon Sep 17 '15 at 04:22
  • You only need to edit the for-loop in randomRange(). Maybe try to paste the function in your browsers javascript console and call randomRange() to see if this new behavior is what you expect to see. – erickb Sep 17 '15 at 04:34
  • Ok- it just distributes all the results evenly. I want x percent for one range, then another percent for another range. Is that possible? I've made a fiddle http://jsfiddle.net/jjz977um/8/ – Ycon Sep 17 '15 at 05:15