从数组中选取3个随机元素。并没有两次获得相同的元素

时间:2016-10-13 10:50:20

标签: javascript

我正在尝试使用此功能打印出三个随机名称而不使用相同的名称两次。我开始尝试在for循环中使用If,但不知道这是否正确。感谢所有帮助,因为我觉得在学习Javascript方面有点困难。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication16
{
    class Program
    {
         static void Main(string[] args)
        {
             DataTable dt = new DataTable();
             dt.Columns.Add("PillarId", typeof(int));
             dt.Columns.Add("Quarter", typeof(string));
             dt.Columns.Add("Feature", typeof(string));

             dt.Rows.Add(new object[] {1, "Q12116", "France"});
             dt.Rows.Add(new object[] {1, "Q12116", "Germany"});
             dt.Rows.Add(new object[] {1, "Q22116", "Italy"});
             dt.Rows.Add(new object[] {1, "Q32116", "Russia"});
             dt.Rows.Add(new object[] {2, "Q22116", "India"});
             dt.Rows.Add(new object[] {2, "Q32116", "USA"});
             dt.Rows.Add(new object[] {3, "Q22116", "China"});
             dt.Rows.Add(new object[] {3, "Q32116", "Austrailia"});
             dt.Rows.Add(new object[] {3, "Q32116", "New Zeland"});
             dt.Rows.Add(new object[] {3, "Q42116", "Japan"});

             string[] uniqueQuarters = dt.AsEnumerable().Select(x => x.Field<string>("Quarter")).Distinct().ToArray();

             var groups = dt.AsEnumerable()
                 .GroupBy(x => x.Field<int>("PillarId")).Select(x => x.GroupBy(y => y.Field<string>("Quarter")).Select(z => new { id = x.Key, quarter = z.Key, feature = z.Select((a,i) => new { feature = a.Field<string>("Feature"), index = i}).ToList()}).ToList()).ToList();

             DataTable pivot = new DataTable();
             pivot.Columns.Add("PillarId", typeof(int));
             foreach (string quarter in uniqueQuarters)
             {
                 pivot.Columns.Add(quarter, typeof(string));
             }

             foreach (var group in groups)
             {
                 int maxNewRows = group.Select(x => x.feature.Count()).Max();
                 for (int index = 0; index < maxNewRows; index++)
                 {
                     DataRow newRow = pivot.Rows.Add();
                     foreach (var row in group)
                     {
                         newRow["PillarId"] = row.id;
                         newRow[row.quarter] = row.feature.Skip(index) == null || !row.feature.Skip(index).Any() ? "" : row.feature.Skip(index).First().feature;
                     }
                 }
             }
        }
    }

}

3 个答案:

答案 0 :(得分:2)

您可以使用do..while循环。另请注意[0].splice()附近返回数组而不是数组的值。

var names = ["Kai", "Lars", "Anders", "Ole"
             , "Petter", "Mikael", "Cos", "Sin"];  
var randName = [];
do {
  randName[randName.length] = names.splice(
                                Math.floor(Math.random() * names.length)
                              , 1)[0];
} while (randName.length < 3);

console.log(randName);

答案 1 :(得分:0)

我建议使用与name不同的变量名,因为它是窗口对象的受保护属性。

&#13;
&#13;
function randomNavn(){
    document.getElementById("utskrift").innerHTML = "";
    for (i = 0; i < 3; i++){
        randName.push(nname.splice(Math.floor(Math.random() * nname.length), 1));
    }
    document.getElementById("utskrift").innerHTML = randName.join(", ");
}

var nname = ["Kai", "Lars", "Anders", "Ole", "Petter", "Mikael", "Cos", "Sin"],
    randName = [];

randomNavn();
&#13;
<div id="utskrift"></div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

对你而言,那将是

var name = ["Kai", "Lars", "Anders", "Ole", "Petter", "Mikael", "Cos", "Sin"];  
var randName = [];
function randomNavn(){
    document.getElementById("utskrift").innerHTML = "";
    for( i = 0; i < 3; i++){
        randName.push(name.splice(Math.floor(Math.random() * name.length), 1));
        if(name[i] === randName[i]){
            i-- // yay, that's all!
        }
    }
    document.getElementById("utskrift").innerHTML = randName.join(" , ");
}

另一个vay:

var i1 = Math.floor(Math.random() * name.length);
var i2 = Math.floor(Math.random() * (name.length-1));
if (i2 >= i1) i2++;
var i2 = Math.floor(Math.random() * (name.length-2));
if (i3 >= i2) i3++;
if (i3 >= i1) i3++;