I have an array :
my Array = new Array ("0","1","2","3");
i want it to generate an index randomnly then it deletes that index so that next time it does not regenerate it. Here my code below, but it does not seems its deleting the index because it generates same index again.

var arr = new Array("0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");
var fill = new Array(20);   

for(var i=0;i<20;i++){ fill[i]=0;}

for (i=0;i<y;i++){
    randno = getRandom(0,20);  //getRandom is function which generate random number
    fill[randno]=1;
    arr.splice(randno, 1);

}

Recommended Answers

All 12 Replies

I really didn't understand what you are trying to do.
You have an array and you want to delete an random item of that array? Is that it?

Member Avatar for diafol

Are you deleting a random item until no items remain?

If so, what are you doing with the array in between deletions?

yes i want it to delete an item from the array along with its index. is it possible? because it should not regenerates the same index next time. I should get a different index each time. If you have a better solution for this , I would apreciate. The index is important for me, i should not get the same index. (index of array)

Let's see if I understood:

// You have an array (I'll use letters to make it more clear)
var myArray = ['a', 'b', 'c', 'd', 'e'];

// Then you want to remove an random index and change the index of all others elements as well
// So the result should be like this
var myResultArray = ['e', 'b', 'd', 'a'];

So, if what you want is on those lines, check this sample out:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var arr = ["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"];

// Remove item from array by index
function removeIndex(arr, index) {
    var len = arr.length;

    // First item
    if ( index == 0 ) {
        return arr.slice(1);
    }
    // Last item
    else if ( len-1 >= index ) {
        return arr.slice(0, len-1);
    }
    // Middle item
    else {
        var arr1 = arr.slice(0, index-1);
        var arr2 = arr.slice(index);

        return arr1.concat(arr2);
    }
}

// Generates random number
function getRandom(min, max) {
    return Math.floor((Math.random()*max)+min);
}

function slice() {  
    // remove random index
    arr = removeIndex(arr, getRandom(0, arr.length));

    // shuffle the rest of the array
    arr.sort(function(){
        return Math.round(Math.random()) - 0.5;
    });

    // log result in div
    var div = document.getElementById("divResult");
    div.innerHTML = div.innerHTML + '<br/>' + arr.toString();
}

</script>

</head>

<body>
    <button onclick="slice();" type="button">Slice and Suffle</button>
    <div id="divResult"></div>
</body>
</html>

var myArray = ['a', 'b', 'c', 'd', 'e'];
the index of the array above is myArray[0] myArray[1] myArray[2] myArray[3] myArray[4].
So i need to return a random index. lets say it returns myArray[1].
So i'm left with those index now myArray[0] myArray[2] myArray[3] myArray[4]. Then it regenerates another index from the remaining and it goes on like this until the array is empty. I think u have done this but shuffle the array is not necessary

why do you need to remove the index?

an object would be your best bet

index or object. it does not matter.

I still don't get what you want, please post examples step by step, like this:

['a', 'b', 'c', 'd']
['a', null, 'c', 'd']
['a', null, null, 'd']

I guess the best way to explain the the trivialities of this problem is this:

var myArray = [0,1,2,3,4,5];
ARRAY: length = 6
index   value
[0]     0
[1]     1
[2]     2
[3]     3
[4]     4
[5]     5

myArray.splice(3,1)
ARRAY: length = 5
index   value
[0]     0
[1]     1
[2]     2
[3]     4
[4]     5

Please check exactly how the index changed when the splice was run, removing one item.

var arr = new Array("0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");
var fill = new Array(20);

//fills "fill" array's values to 0 
for(var i=0;i<20;i++){ fill[i]=0;}

// y has no referenced value, what's the value of 'y'?
for (i=0;i<y;i++){
    randno = getRandom(0,20);  //getRandom is function which generate random number
    //could reassing 1 to an already 1
    // since random number's use, a number can be repeated multiple times
    // an index can be accessed multiple times
    fill[randno]=1;
    //As explained above, index will change and length
    arr.splice(randno, 1);
}

//Possible Expected Output - (guess)
// fill - all array values equal to 1
// arr - empty array

The problem with the given example are the following:

  • what's the point of filling a new array with (fill) zeros when in the end, you'll just make all the values to 1.
  • What does the y do? is it something used to know if all the values in fill are '1'?
  • Why should all values of arr be removed?
  • Where's the logic in putting fill with a value of 1 while removeing the value of arr in the same index? As explained above, the length difference of both could be detrimental when refercing on both array's indexes once the splice's used.

Anyway, I believe that's a lot of question. LOL
You can just answer this one, what's your main goal in you snippet? or as a whole.

Member Avatar for stbuchok

I believe this is what you are looking for:

<html>
<head>

<script>

var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

//keep looping until the array is empty
while(arr.length > 0){
    var randomNum = Math.floor(Math.random() * arr.length);

    //get the random index to start at.
    for(var i = randomNum; i < arr.length - 1; i++){
        //override the current index (i) with the next index (i+1)
        arr[i] = arr[i+1];
    }

    //drop the last item in the array (no longer needed)
    arr.pop();
}

</script>

</head>
<body>

</body>
</html>

This will randomly remove an item from the array. It isn't exactly fast, but if you have an array under 1000, this should do the trick.

Member Avatar for diafol
var numArray = [1,2,3,4,5,6,7,8,9,10];
maxRnd = numArray.length;
while(numArray.length>0){
    document.write('<br />' + numArray );
    rnd = Math.floor(Math.random()*maxRnd)
    p = numArray.splice(rnd,1);
    document.write('<br />Removed ' + p + ': ');
    maxRnd--;
}

//edit

Similar to st's I think.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.