简体   繁体   English

在给定列表之外生成随机整数<int></int>

[英]Generate Random int number outside given List<int>

I am currently trying to find a way to generate a random number outside given list.我目前正在尝试找到一种在给定列表之外生成随机数的方法。

I did this我做了这个

public static int getNewNumberOutsideGivenList(List<int> _list, int _min, int _max)
{
    int a = 0;
    // I use "Unity.Random", but as C# user you guys can use System.Random function
    a = UnityEngine.Random.Range(_min, _max +1); 
    while (_list.Contains(a))
        a = UnityEngine.Random.Range(_min, _max + 1);

    return a;
}

It works(kinda) well (I got a short drop in FPS like 10ms after calling this function).它工作(有点)很好(在调用这个函数后,我的 FPS 有一个短暂的下降,比如 10 毫秒)。

However i keep wondering, "is there a better way to do this?"但是我一直想知道,“有没有更好的方法来做到这一点?” or "does System.Linq has a function that is faster than that code?"或“System.Linq 是否有一个比该代码更快的 function?”

Can you please enlightened me if there exist a better way?如果有更好的方法,你能告诉我吗? Thank yoouuu for your insight.谢谢你的洞察力。 :) :)

IMHO you should pre intialize all possible values and than shuffle the list.恕我直言,您应该预先初始化所有可能的值,然后重新排列列表。

using System;
using System.Collections.Generic;
using System.Linq;

                    
public class Program
{
    static Queue<int> _randomNumbers;
    static Program()
    {
        var min = 0;
        var max = 10;
        var list = new List<int>();
        for (int i = min; i < max; i++)
            list.Add(i);
        
        _randomNumbers = new Queue<int>(list.OrderBy(x => Guid.NewGuid()));
    }
    
    public static void Main()
    {
        while (true)
        {
            var next = GetNext();
            if (next is null)
                break;
            
            Console.WriteLine(next);
        }
    }
    
    static int? GetNext() => _randomNumbers.Count == 0 ? null : _randomNumbers.Dequeue();
}

https://dotnetfiddle.net/pBsmyH https://dotnetfiddle.net/pBsmyH

You may find a better shuffle alternative here:您可以在这里找到更好的 shuffle 替代方案:

Randomize a List<T>随机化一个列表<T>

Edit:编辑:

Here a version without the List<int> but instead it uses Enumerable.Range(start, count);这里没有List<int>的版本,而是使用Enumerable.Range(start, count);

_randomNumbers = new Queue<int>(Enumerable.Range(min, max - min).OrderBy(x => Guid.NewGuid()));

https://dotnetfiddle.net/S8aDuE https://dotnetfiddle.net/S8aDuE

Edit2:编辑2:

By popular demand here a version with one of the better shuffle alternatives根据大众的需求,这里有一个更好的洗牌替代品之一的版本

using System;
using System.Collections.Generic;
using System.Linq;

                    
public class Program
{
    static Queue<int> _randomNumbers;
    static Random rng = new Random();  
    
    static Program()
    {
        var min = 0;
        var max = 10;
        var list = new List<int>();
        for (int i = min; i < max; i++)
            list.Add(i);
        
        Shuffle(list);
        _randomNumbers = new Queue<int>(list);
            
        static void Shuffle<T>(IList<T> list)  
        {  
            int n = list.Count;  
            while (n > 1) {  
                n--;  
                int k = rng.Next(n + 1);  
                (list[k], list[n]) = (list[n], list[k]); //Notice: I incorporated @MatthewWatson's idea, so it is slightly different than the one you find on the posted SO page
            }  
        }
    }
    
    public static void Main()
    {
        while (true)
        {
            var next = GetNext();
            if (next is null)
                break;
            
            Console.WriteLine(next);
        }
    }
    
    static int? GetNext() => _randomNumbers.Count == 0 ? null : _randomNumbers.Dequeue();
}

https://dotnetfiddle.net/FRKYyA https://dotnetfiddle.net/FRKYyA

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

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