C#代码显示2个不同范围的随机数。像range1(0-10)和range2(11-12) [英] C# code to display random numbers for 2 different range. Like range1(0-10) and range2(11-12)

查看:515
本文介绍了C#代码显示2个不同范围的随机数。像range1(0-10)和range2(11-12)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

i需要ac#代码来显示两个范围内的随机数。

它应该像这样显示。范围1(0,5)范围2(6 ,10)并且他们不得重复

 range1 range2 
1 6
2 7
3 8
4 9
5 10





我的尝试:



随机rand =  new  Random(); 
List< int> randomNumbers = new List< int>();

int range1;
int range2;

for int i = 0 ; i < 6 ; i ++)
{
do
{
range1 = rand.Next( 0 10 );

} while (randomNumbers.Contains(range1));
randomNumbers.Add(range1);
Console.WriteLine( {0},range1);

}

随机数= Random();
List< int> number = new List< int>();

for int j = 0 ; j < 6 ; j ++)
{
do
{
range2 = num.Next( 11 20 );


} while (number.Contains(range2));
number.Add(range2);

Console.WriteLine( {0},range2);

}

Console.ReadLine();
}

解决方案

如果我这样做,我会创建两个基于哈希的集合。对于两组集合,我会创建一个随机数并尝试将其添加到集合中 - 如果由于数字已经存在而无法添加它,我会添加另一个。您实施的控制是每个集合的大小基于第一个和第二个数字之间的差异,因此range1将基于5到0之间的距离。现在您知道如何做到这一点,所有你是要做的就是编写代码(作为提示,这里有一个有用的基于散列的集合将是HashSet< int>)。


问题是不重复位 - 随机数是只是:随机。所以设置一个不应该重复的约束会使问题变得复杂,因为你不能直接使用随机数。

你需要做的是设置两个List,每个包含范围。这很容易做到,你已经在那里了一半:

 List< int> range1 = Enumerable.Range( 0  6 )。ToList(); 
List< int> range2 = Enumerable.Range( 6 4 )。ToList();



然后根据随机索引取出数字并将其从相应的列表中删除:

随机rand =  new  Random(); 
for int i = range1.Count - 1 ; i > = 0 ; i--)
{
int randomIndex = rand.Next(range1.Count);
Console.WriteLine(range1 [randomIndex]);
range1.RemoveAt(randomIndex);
}


引用:

他们不得重复



这使得它不是随机的,是shuffle 。就像卡片一样。



程序的问题在于它选择值时会退化,选择未使用的程序会越来越困难。



还有另一种洗牌方法。

假设您要洗牌52张牌。

- 列出一个数组中的52个值。

- 选择0到51之间的随机位置并将该位置与位置0交换。

- 选择1到51之间的随机位置并交换位置为1的位置。

- 选择2到51之间的随机位置并将该位置与位置2交换。

- 重复直到足够的牌被洗牌。

- 读取数组,这是你的结果。



优点,它不退化,对于52张卡,你需要51次使用随机功能


hi everyone
i need a c# code to display random numbers from two ranges.
it shou be displayed like this.the range1(0,5) range2(6,10) and they must not repeat

range1        range2
1             6
2             7 
3             8
4             9   
5             10



What I have tried:

    Random rand = new Random();
    List<int> randomNumbers = new List<int>();

    int range1;
    int range2;

    for (int i = 0; i < 6; i++)
    {
        do
        {
            range1 = rand.Next(0, 10);

        } while (randomNumbers.Contains(range1));
        randomNumbers.Add(range1);
        Console.WriteLine("{0}", range1);

    }

    Random num = new Random();
    List<int> number = new List<int>();

    for (int j = 0; j < 6; j++)
    {
        do
        {
            range2 = num.Next(11, 20);


        } while (number.Contains(range2));
        number.Add(range2);

        Console.WriteLine("{0}", range2);

    }

    Console.ReadLine();
}

解决方案

If I were doing this, I would look to create two hash based collections. For both sets of collections, I would create a random number and attempt to add it to the collection - if I can't add it because the number is already present, I would add another. The control you put in place is that the size of each collection is based on the difference between the first and second number, so range1 would be based on the distance between 5 and 0. Now you know how to do it, all you're left to do is code it up (as a hint, a useful hash based collection here would be HashSet<int>).


The problem is the "not repeat" bit - random numbers are just that: random. So putting a constraint that they should not repeat complicates the issue because you can't use random numbers directly.
What you need to do is set up two List, each containing the range. That's easy to do, you're halfway there already:

List<int> range1 = Enumerable.Range(0, 6).ToList();
List<int> range2 = Enumerable.Range(6, 4).ToList();


Then pull out the numbers according to a random index and remove it from the appropriate list:

Random rand = new Random();
for (int i = range1.Count - 1; i >= 0; i--)
    {
    int randomIndex = rand.Next(range1.Count);
    Console.WriteLine(range1[randomIndex]);
    range1.RemoveAt(randomIndex);
    }


Quote:

they must not repeat


This make that it is not random, it is shuffle. Just like with cards.

The problem with your program is that it degenerate as it pick values, it is more and more difficult to pick unused ones.

There is another technique to shuffle values.
Say you want to shuffle 52 cards.
- make a list of the 52 values in an array.
- pick a random position between 0 and 51 and swap that position with position 0.
- pick a random position between 1 and 51 and swap that position with position 1.
- pick a random position between 2 and 51 and swap that position with position 2.
- repeat until enough cards are shuffled.
- read the array, it is your result.

Advantage, it don't degenerate, for 52 cards, you need 51 uses of random function.


这篇关于C#代码显示2个不同范围的随机数。像range1(0-10)和range2(11-12)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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