C#中的唯一随机数 [英] Unique random numbers in C#

查看:116
本文介绍了C#中的唯一随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建具有唯一随机数的二叉搜索树.我使用SortedSet表示我的树,然后将其放入数组中,然后使用Contains来查看树中是否有特定的数字.我的问题是我无法弄清楚如何以一种简单的方式来获得所有不同的随机数.我使用了Unik和Nålen_Unik的方法,但是在这段代码中,它只为数组生成1个数字

        Random random = new Random();
        Program Tal = new Program();
        string nål = Tal.Nålen_Unik();
        string TalIArray = Tal.Unik();
        bool found = false;
        SortedSet<string> Tree = new SortedSet<string>();
        for (int x = 0; x < 50000; x++)
        {
            Tree.Add(TalIArray);
        }
        int y = 0;
        string[] TreeArray = Tree.ToArray<string>();
        while (y < TreeArray.Length)
        {
            Console.WriteLine(TreeArray[y]);
            y = y + 1;
        }

  private string Unik()
    {
        int maxSize = 4;

        char[] chars = new char[10000];

        string a;
        a = "0123456789";

        chars = a.ToCharArray();

        int size = maxSize;

        byte[] data = new byte[1];

        RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
        crypto.GetNonZeroBytes(data);

        size = maxSize;
        data = new byte[size];
        crypto.GetNonZeroBytes(data);

        StringBuilder result = new StringBuilder(size);

        foreach (byte b in data)
        {
            result.Append(chars[b % (chars.Length - 1)]);
        }

        return result.ToString();
    }
    private string Nålen_Unik()
    {
        int maxSize = 1;

        char[] chars = new char[62];

        string a;
        a = "0123456789";

        chars = a.ToCharArray();

        int size = maxSize;

        byte[] data = new byte[1];

        RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
        crypto.GetNonZeroBytes(data);

        size = maxSize;
        data = new byte[size];
        crypto.GetNonZeroBytes(data);

        StringBuilder result = new StringBuilder(size);

        foreach (byte b in data)
        {
            result.Append(chars[b % (chars.Length - 1)]);
        }

        return result.ToString();

解决方案

主要有三种方法可用于获取无冲突的随机数:

  1. 保留选择的所有号码,以便您可以对照以前的所有号码检查新号码.
  2. 创建一系列唯一数字,将其洗牌,然后一次从结果中选择一个.
  3. 创建一个如此大的随机数,以至于碰撞的风险很小,可以忽略不计.

第二种方法与洗牌组的原理相同.创建 GUID 时,使用第三种方法,该值基本上是一个随机的128位值.

I'm trying to create a binary search tree with unique random numbers. I'm using SortedSet to represent my tree and then I make it into an array and then I use Contains to see if a certain number is in the tree. My problem is that I can't figure out how to get all the random numbers different in a simple way. I used the methods Unik and Nålen_Unik but in this code it only generates 1 number to the array

        Random random = new Random();
        Program Tal = new Program();
        string nål = Tal.Nålen_Unik();
        string TalIArray = Tal.Unik();
        bool found = false;
        SortedSet<string> Tree = new SortedSet<string>();
        for (int x = 0; x < 50000; x++)
        {
            Tree.Add(TalIArray);
        }
        int y = 0;
        string[] TreeArray = Tree.ToArray<string>();
        while (y < TreeArray.Length)
        {
            Console.WriteLine(TreeArray[y]);
            y = y + 1;
        }

  private string Unik()
    {
        int maxSize = 4;

        char[] chars = new char[10000];

        string a;
        a = "0123456789";

        chars = a.ToCharArray();

        int size = maxSize;

        byte[] data = new byte[1];

        RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
        crypto.GetNonZeroBytes(data);

        size = maxSize;
        data = new byte[size];
        crypto.GetNonZeroBytes(data);

        StringBuilder result = new StringBuilder(size);

        foreach (byte b in data)
        {
            result.Append(chars[b % (chars.Length - 1)]);
        }

        return result.ToString();
    }
    private string Nålen_Unik()
    {
        int maxSize = 1;

        char[] chars = new char[62];

        string a;
        a = "0123456789";

        chars = a.ToCharArray();

        int size = maxSize;

        byte[] data = new byte[1];

        RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
        crypto.GetNonZeroBytes(data);

        size = maxSize;
        data = new byte[size];
        crypto.GetNonZeroBytes(data);

        StringBuilder result = new StringBuilder(size);

        foreach (byte b in data)
        {
            result.Append(chars[b % (chars.Length - 1)]);
        }

        return result.ToString();

解决方案

There are mainly three approaches that are used to get random numbers without collisions:

  1. Keep all numbers that you have picked, so that you can check a new number against all previous.
  2. Create a range of unique numbers, shuffle them, and pick one at a time from the result.
  3. Create such a huge random number that the risk of collisions is so small that it's negligible.

The second approach is the same principle as shuffling a deck of cards. The third approach is used when creating a GUID, which is basically a random 128 bit value.

这篇关于C#中的唯一随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆