需要在C#中从给定范围生成一对数字

时间:2016-12-03 15:25:11

标签: c# random

好吧,我需要生成(1,8)范围内的2个数字。 假设,前两个数字是3&在第二次,范围将排除3& 4,这就是我想要完成的。这样循环将继续4次。

我一直在尝试......但是无法继续进行 我所知道的就是使用#include <iostream> #include <vector> #include <string> using namespace std; class class1 { public: void printVector(std::vector<string>& names); }; void class1::printVector(vector<string>& names) { for (unsigned int i = 0; i < names.size(); i++) { cout << names[i] << endl; } } int main() { vector<string> names; names.push_back("Bob"); names.push_back("Gorge"); names.push_back("Bill"); names.push_back("Freddy"); names.push_back("Daniel"); names.push_back("Emily"); class1 obj; obj.printVector(names); return 0; } Class

范围是固定的

任何类型的帮助都将被接受。谢谢提前

2 个答案:

答案 0 :(得分:3)

方法1(经典):

static List<Tuple<int, int>> GetPairs(int min, int max, Random r)
{
    var items = new List<Tuple<int, int>>();
    var pickedItems = new HashSet<int>();
    int count = (max - min + 1);

    Func<int> randAndCheck = () =>
    {
        int? candidate = null;

        while(candidate == null || pickedItems.Contains(candidate.Value))
            candidate = r.Next(min, max + 1);

        pickedItems.Add(candidate.Value);
        return candidate.Value;
    };

    while (pickedItems.Count != count)
    {
        int firstItem = randAndCheck();
        int secondItem = randAndCheck();

        items.Add(Tuple.Create(firstItem, secondItem));
    }

    return items;
}

<强>用法:

static void Main(string[] args)
{
    foreach(var pair in GetPairs(1, 8, new Random()))
    {
        Console.WriteLine($"One: {pair.Item1} Two: {pair.Item2}");
    }
}

<强>输出:

One: 4 Two: 2
One: 8 Two: 3
One: 5 Two: 1
One: 7 Two: 6

以某种方式随机排序和执行不同的方法2:

static IEnumerable<Tuple<int, int>> TwoAtATime(int min, int max, Random r)
{
    var enumerator = Enumerable.Range(min, max - min + 1)
                                .OrderBy(x=> r.Next()).GetEnumerator();

    while(enumerator.MoveNext())
    {
        int item1 = enumerator.Current;
        if (enumerator.MoveNext())
        {
            int item2 = enumerator.Current;
            yield return Tuple.Create(item1, item2);
        }
    }
}

<强>用法:

static void Main(string[] args)
{
    foreach(var pair in TwoAtATime(1, 8, new Random()))
    {
        Console.WriteLine($"One: {pair.Item1} Two: {pair.Item2}");
    }
}

<强>输出:

One: 2 Two: 5
One: 4 Two: 7
One: 1 Two: 6
One: 8 Two: 3

答案 1 :(得分:0)

以下是代码:

using System.Collections.Generic;
namespace TestApp
{
    class Program
    {
        static readonly Random rand = 
            new Random(DateTime.Now.Millisecond);

        static void Main(string[] args)
        {
            var pickList = new List<int> {1, 2, 3, 4, 5, 6, 7, 8};
            while (pickList.Count > 1)
            {
               Console.WriteLine(
                  "Hit any key to get Random pair of integers from List.");
               Console.ReadLine();
               var pair = GetPairFromList(pickList);
               Console.WriteLine($"Pair is: {pair.Item1}: {pair.Item2}");
            }
            Console.WriteLine("List is empty; Hit any key to exit.");
            Console.ReadLine();
        }
        private static Tuple<int, int> GetPairFromList(IList<int> picks)
        {
            return new Tuple<int, int>(
                GetIntFromList(picks), 
                GetIntFromList(picks));
        }
        private static int GetIntFromList(IList<int> picks)
        {
            // next line is where random picking happens
            var val = picks[(int)(picks.Count*rand.NextDouble())];
            // remove randomly selected item from list
            picks.Remove(val);
            return val;
        }
    }
}