简体   繁体   English

如何在1-100之间创建1000个随机整数的数组

[英]How do you make an array of 1000 random integers between 1-100

How do you program c# to make an array of 1000 random integers between 1-100. 如何编程c#以在1-100之间创建1000个随机整数的数组。

And then how do you get when a person enters a number eg 68 how can you make the program say 68 appears so and so many times or that it doesn't work at all! 然后你怎么得到一个人输入一个数字,例如68你怎么能让程序说68出现这么多次或根本不起作用!

I am not asking for the complete answer I just need a hint where to get started. 我不是要求完整的答案我只需要提示从哪里开始。

Here is what I know: 这就是我所知道的:

I have to use the random function and an if but I dont know what to put where! 我必须使用随机函数和if但我不知道放在哪里!

int[] iArray = new int[1000];
int counter = 0;
Random random = new Random();
for(int i = 0; i < 1000; i++){
   iArray[i] = random.Next(1, 101); //1 - 100, including 100
}
int number = Convert.ToInt32(Console.ReadLine());
foreach(int i in iArray){
  if(i == number)count++;
}
Console.WriteLine("The number "+ number+" appears "+count+" times!");

Start with a for loop , in each iterative you call the random function and put the result in a public list . for loop开始,在每次迭代中调用random function并将结果放在public list After that you make an dialog for the user to type a number. 之后,您将为用户创建一个对话框以键入数字。 You can serach with lambda expression in the list to see how many matches you get. 您可以在列表中搜索lambda expression以查看您获得的匹配项数量。

Make an array of 1000 random integers between 1-100 and when a person enters a number eg 68 how can you make the program say 68 appears so and so many times 在1-100之间创建1000个随机整数的数组,当一个人输入一个数字,例如68,你怎么能让程序说68出现这么多次

I think you're looking for a method like this: 我想你正在寻找这样的方法:

private static Random rnd = new Random();

public static IEnumerable<int> getRandomNumbers(int count, int lowerbound, int upperbound, int specialNumber = int.MinValue, int specialNumberCount = int.MinValue)
{
    List<int> list = new List<int>(count);
    HashSet<int> specialNumPositions = new HashSet<int>();

    if (specialNumberCount > 0)
    {
        // generate random positions for the number that must be create at least n-times
        for (int i = 0; i < specialNumberCount; i++)
        {
            while (!specialNumPositions.Add(rnd.Next(0, count)))
                ;
        }
    }

    while (list.Count < count)
    {
        if (specialNumPositions.Contains(list.Count))
            list.Add(specialNumber);
        else
            list.Add(rnd.Next(lowerbound, upperbound + 1));
    }
    return list;
}

which you can use in this way: 您可以这样使用:

// ensure that 68 is generated at least 10 times
var list = getRandomNumbers(1000, 1, 100, 68, 10);

Demo 演示

If you instead just want to know how often a number appears in the list, you can ue Linq: 如果你只是想知道一个数字出现在列表中的频率,你可以使用Linq:

int count = list.Count(i => i == 68);

 array = [...Array(1000).keys()].sort((x, y) => { let ren = Math.random() if (ren == 0.5) return 0; return ren > 0.5 ? 1 : -1 }) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM