如何生成随机数并将其插入优先级队列?

时间:2019-04-07 08:26:18

标签: c# priority-queue random-seed

我已经使用带有所需功能的SLList实现了PriorityQueue。我想生成100个随机整数并将其添加到队列(类PriorityQueue的对象)中。然后从队列中输出前20个数字。

我尝试使用RandomGenerator()生成随机数,但是它总是提供相同的随机数。生成100个随机数的循环工作正常,但始终推送相同的随机数。如何生成随机数并将其插入优先级队列?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Class1
    {

        public class Node
        {
            public int data;
            public int priority;

            public Node next;
        }

        public static Node node = new Node();
        public static Node newNode(int d, int p)
        {
            Node temp = new Node();
            temp.data = d;
            temp.priority = p;
            temp.next = null;

            return temp;
        }

        public static int peek(Node head)
        {
            return (head).data;
        }

        public static Node pop(Node head)
        {
            Node temp = head;
            (head) = (head).next;
            return head;
        }
        public static Node push(Node head,int d, int p)
        {
            Node start = (head);
            Node temp = newNode(d, p);
            if ((head).priority > p)
            {

                // Insert New Node before head  
                temp.next = head;
                (head) = temp;
            }
            else
            {
                while (start.next != null &&
                       start.next.priority < p)
                {
                    start = start.next;
                }

                // Either at the ends of the list  
                // or at required position  
                temp.next = start.next;
                start.next = temp;
            }
            return head;
        }

        public static int isEmpty(Node head)
        {
            return ((head) == null) ? 1 : 0;
        }
        public class RandomGenerator
        {
            // Generate a random number between two numbers    
            public int RandomNumber(int min, int max)
            {
                Random random = new Random();
                return random.Next(min, max);
            }
            public string RandomPassword()
            {
                StringBuilder builder = new StringBuilder();
                builder.Append(RandomNumber(1000, 9999));
                return builder.ToString();
            }
        }
        public static void Main(string[] args)
        {
            /*
            Node pq = newNode(4, 1);
            pq = push(pq, 5, 2);
            pq = push(pq, 6, 3);
            pq = push(pq, 7, 0);


            while (isEmpty(pq) == 0)
            {
                Console.Write("{0:D} ", peek(pq));
                pq = pop(pq);
            } */
            RandomGenerator generator = new RandomGenerator();
            Node pq = newNode(4, 0);
            int p = 1;
            // Console.WriteLine($"Random number  is {rand}");
            for (int i = 0; i < 100; i++)
            {
                int rand = generator.RandomNumber(0, 1000000);

                pq = push(pq, rand, p);
                p = p + 1;
            }
            while (isEmpty(pq) == 0)
            {
                Console.Write("{0:D} ", peek(pq));
                pq = pop(pq);
            }
        }
           // Console.ReadKey();
        }
    }

我希望输出: 8 200 1 2 5 4 3 ...(即生成的任何随机数) 而我得到的输出: 6200200200200200 ...(即,将相同的随机数推送到优先级队列中)

1 个答案:

答案 0 :(得分:2)

创建Random对象时,其种子将根据当前时间进行初始化。如果同时创建多个Random对象,则它们将全部使用相同的种子进行初始化,并在调用Next时返回相同的数字。

相反,您应该只构造一个Random对象,然后在同一对象上多次调用Next

    public class RandomGenerator
    {
        private Random random = new Random();

        // Generate a random number between two numbers    
        public int RandomNumber(int min, int max)
        {
            return random.Next(min, max);
        }