0

Ok so guys I'm having trouble with random numbers inside a function in javascript. I want to produce an array that contains between 3 to 7 indexes or whatever you call that. I mean like this arr[value,value,value] -> arr[value,value,value...]. I already done that. my main problem is that when I try to put each value of this array to another array, I sometimes get a empty value at the end. I mean when I print the array I sometimes get this 20,11,17, . I tried to use pop method but when I used that I only got 2 numbers, which is 1 less than the minimum number that I need. Please help, I have my code below

function randGen(num){
            var a = Math.floor((Math.random() * 5) + 3);
            var c = num.length;
            var d;
            var arr = [];
            var mainArr = [];
            for(var b = 0; b < a; b++){
                d = Math.floor(Math.random() * (c+1));
                for(var t = 0; t < arr.length; t++){
                    while(d == arr[t]){
                        d = Math.floor(Math.random() * (c+1));
                    }
                }
                arr.push(d);
            }
            for(var qw = 0; qw < arr.length; qw++){
               mainArr[qw] = num[arr[qw]]; 
            }
            mainArr.sort();

            document.write(mainArr);
        }

 $(function (){       

            var num = [];
            for(var a = 5; a <= 100; a+=5){
                num.push(a);
            }

            randGen(num);

        });

Sometimes results to this: 5,10,25,70, Pop method did not do the work as I said earlier, so any help would be much appreciated.

P.S num.length = 20;

1
  • How about randmoize your primary array and then get n(random number between your range) number of elements from that array ? Commented Aug 1, 2015 at 5:44

1 Answer 1

1

After spending an hour or so I got this right. Try this:

function randGen(num) {
    var a = Math.floor((Math.random() * 5) + 3);
    var c = num.length;
    var d;
    var arr = [];
    var mainArr = [];
    for (var b = 0; b < a; b++) {
        d = Math.floor(Math.random() * (c));
        for (var t = 0; t < arr.length; t++) {
            while (d == arr[t]) {
                d = Math.floor(Math.random() * (c));
            }
        }
        arr.push(d);
    }
    for (var qw = 0; qw < arr.length; qw++) {
        mainArr[qw] = num[arr[qw]];
    }
    mainArr.sort();
    console.log(mainArr);
}
$(function () {
    var num = [];
    for (var a = 5; a <= 100; a += 5) {
        num.push(a);
    }
    randGen(num);
});

num.length in the length of your input array and while fetching random index from that array you need to use array.lenght-1 as index starts from 0. I have made the required modifications in your code.

Sign up to request clarification or add additional context in comments.

1 Comment

Geezzzz thanks so much :) Didn't notice that. You earned my respect Sir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.