Angular 2'de tekrar etmeden bir dizi oluşturma


1'den 60'a kadar herhangi bir tekrarlayan sayı içermemesi gereken altı elemanlı bir dizi oluşturmaya çalışıyorum.

Benim kodum şu anda böyle:

 jogar(){        
    if(this.random.length != 0){
        this.random=[];
        }else{
            for(var u=0; u<6; u++){
             this.y = Math.ceil(Math.random()*59+1);
                for (var r of this.random){
                    if(r != this.y){
                        this.random.push(this.y); 
                    };  
                };                                                              
            }; 
            this.random.sort(function(a, b){return a-b});
            return this.random; 
        };                                 
};

Dizinin 0'dan fazla elemanı olup olmadığını kontrol etmelidir, eğer varsa boşaltacaktır. Eğer boşsa diziyi oluşturacaktır... Ve sayıları tekrarlamamalıdır... Bu, tekrar eden kontrolü eklediğim noktaya kadar çalışıyor..:

               for(var r of this.random)
                if(r != this.y){
                    this.random.push(this.y); 
                }; 

Bu adam kodumun çalışmasını durduruyor.

Araştırdıktan sonra Fisher-Yates Karıştırma tekniği hakkında bir şeyler okudum, ama benim için işe yaramıyor gibi görünüyor.

Angular 2 kullanıyorum, Ionic 2 framework ile birlikte.

Buradaki biri bunu zaten buldu mu?

Cevap #1

O cümle yanlış çünkü

for(var r of this.random)
  if(r != this.y){
    this.random.push(this.y); 
}; 

this.y sayısını, rastgele dizideki bir sayı this.y sayısından farklı olduğu her seferinde rastgele diziye eklemeye çalışacak.

Bu çalışan plunker'da görebileceğiniz gibi, aşağıdaki kod ile aradığınızı elde edebilirsiniz:

import { Component } from '@angular/core';

@Component({...})
export class HomePage {

  random: Array<number> = [];


  public jogar(): Array<number> {        
    if(this.random.length != 0) {
      this.random=[];
    }

    while (this.random.length < 6) { // <- Use while instead of for
      let randomNumber = this.getRandomNumber(); // Get a random number
      if(!this.wasAlreadyAdded(randomNumber)) {
        this.random.push(randomNumber); // Add it if it doesn't exist in the array
      }
    }

    this.random.sort(function(a, b){return a-b});
    return this.random; 

  }  

  private getRandomNumber(): number {
    return Math.ceil(Math.random()*59+1);
  }

  private wasAlreadyAdded(randomNumber: number): number {
    return this.random.indexOf(randomNumber) !== -1;
  }

}

Cevap #2

İşte kodumun şu anki hali. Çalışıyor!

jogar(){    
    this.final = [];
    this.numeros = [];  
    for(var i=0;this.final.length<6;i++){
        this.final = this.numberSelect.map(Number).concat(this.numeros);           
        var novoNumero=Math.floor((Math.random()*59)+1);            
        var verificacao=this.confereSeArrayContemItem(this.final,novoNumero);       
        if(verificacao!==1){
            this.numeros.push(novoNumero);                
        }else{
            i--;
        }
        this.final.sort(function(a,b){return a-b});      
    } 
    console.log(this.final.sort(function(a,b){return a-b}));
}

confereSeArrayContemItem(array, item){
    var resultado = 0;
    for(var i=0;i<array.length;i++){
        if(array[i]===item){
            resultado =1;
        }
    }
    return resultado;
}