• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

javascript array without duplicates

How can I create a javascript array with a variable quantity without any duplicates?

function randomFromInterval(from, to, qty) {

    var arr = [];

    for (var i=0; i <= qty; i++) {	

        arr[i] = Math.floor(Math.random() * (to - from + 1) + from);	

    }

    return arr;

}

Open in new window


I'm so confused by this. I've locked up my browser with infinite loops so many times. thanks so much for helping!
0
hibbsusan
Asked:
hibbsusan
  • 8
  • 5
  • 4
1 Solution
 
Michel PlungjanIT ExpertCommented:
Thousands on the web

This one seems quite elegant

http://stackoverflow.com/a/2380349/295783
0
 
leakim971PluritechnicianCommented:
Try this :

    Array.prototype.inArray = function(v) {
        for(var i=0;i<this.length;i++) if(this[i] === v) return true;
        return false;
    }

    function randomFromInterval(from, to, qty) {
    
        var arr = [];
    
        while(arr.length<=qty) {    
            var r = Math.floor(Math.random() * (to - from + 1) + from);
            if(!arr.inArray(r)) arr.push(r);    
    
        }
    
        return arr;

    }
 

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
You mean indexOf since inArray is jQuery
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
hibbsusanAuthor Commented:
leakim971,

That seems very simple and really great! Thanks!

How does prototype work?
0
 
hibbsusanAuthor Commented:
that prototype thing kind of broke the rest of my code though. now the page won't load.

can i use it in such a way that it won't affect any other code?
0
 
leakim971PluritechnicianCommented:
Please provide a link to see your page
test page here : http://jsfiddle.net/VwFDv/
0
 
hibbsusanAuthor Commented:
This is with the randomFromInterval() function at the top of this page that I started out with

Here's where I installed the function you've supplied
0
 
leakim971PluritechnicianCommented:
you should replace :  while(arr.length<=qty) {  
by :  while(arr.length<qty) {  

but it will not fix your code.
I think the problem come from the fading process
0
 
hibbsusanAuthor Commented:
so there's not really a good way to do this without using prototype, in your opinion?
0
 
Michel PlungjanIT ExpertCommented:
Prototype is just one method to add a method to an object
You can use the same function without using the prototype.

Here is one using indexOf

function randomFromInterval(from, to, qty) {
  var r,arr = [];
  while(arr.length<=qty) {    
    r = Math.floor(Math.random() * (to - from + 1) + from);
    if(!arr.indexOf(r)) arr.push(r);    
  }
  return arr;
 }

Open in new window

0
 
hibbsusanAuthor Commented:
this freezes the browser for me

http://jsfiddle.net/loren_hibbard/FkaSj/
0
 
Michel PlungjanIT ExpertCommented:
Can you change from mootools to plain js?
I cannot test jsfiddle on my iphone
0
 
hibbsusanAuthor Commented:
0
 
hibbsusanAuthor Commented:
for some reason, this works
if( (arr.indexOf(r)) === -1 ){ 
	
	arr.push(r); 
	
	}

Open in new window


dunno why it didn't before with just if ( !arr.indexOf(r))..
0
 
Michel PlungjanIT ExpertCommented:
Oops sorry. Yes -1 is the test
0
 
hibbsusanAuthor Commented:
great. This was my final solution.

function randomFromInterval(from, to, qty, duplicates) {

    var r, arr = [];

    while (arr.length <= qty) {

        r = Math.floor(Math.random() * (to - from + 1) + from);

        if (!duplicates) {
           
            if ( (to-from) < qty ) {
               
                alert('not enough to choose from');
               
                return false;  
               
            }

            if ((arr.indexOf(r)) === -1) {

                arr.push(r);

            }

        } else {
           
            arr.push(r);
           
        }
    }

    return arr;

}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now