I have implemented a PriorityQueue using a SLList with the required functions. I want to generate 100 random integers and add them to the queue (an object to the class PriorityQueue). Then output the first 20 numbers from the queue.
I have tried to generate Random numbers using RandomGenerator() but it always gives the same random number. The loop to generate 100 random numbers works fine but it always pushes the same random number. How do I generate random numbers and insert them into the priority queue?
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();
}
}
I expect an output of : 8 200 1 2 5 4 3... (i.e. any random numbers generated) whereas I get an output of : 6 200 200 200 200 200...(i.e. the same random number being pushed to the priority queue)
When you create a Random
object, its seed is initialized based on the current time. If you create many Random
objects all at the same time, they will all initialize with the same seed, and return the same number when you call Next
.
Instead, you should construct just one Random
object, and then call Next
multiple times on the same object:
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);
}