如何生成随机数并将它们插入优先级队列?
How do I generate random numbers and insert them to a priority queue?
我已经使用具有所需功能的 SLList 实现了 PriorityQueue。我想生成 100 个随机整数并将它们添加到队列(class 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...(即生成的任何随机数)
而我得到的输出是:
6 200 200 200 200 200...(即相同的随机数被推入优先级队列)
当您创建一个 Random
对象时,它的种子会根据当前时间进行初始化。如果您同时创建许多 Random
个对象,它们都将使用相同的种子初始化,并且 return 在您调用 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);
}
我已经使用具有所需功能的 SLList 实现了 PriorityQueue。我想生成 100 个随机整数并将它们添加到队列(class 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...(即生成的任何随机数) 而我得到的输出是: 6 200 200 200 200 200...(即相同的随机数被推入优先级队列)
当您创建一个 Random
对象时,它的种子会根据当前时间进行初始化。如果您同时创建许多 Random
个对象,它们都将使用相同的种子初始化,并且 return 在您调用 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);
}