C
C#10mo ago
Max

Infinite loop

c#
/// ------------------------------------------------------------
/// <summary>
/// Creates a stack containing unique random numbers</summary>
/// <param name="lower">The lower bound for each number (inclusive)</param>
/// <param name="upper">The upper bound for each number (inclusive)</param>
/// <param name="count">The number of numbers</param>
/// <returns>The stack containing unique random numbers</returns>
/// ------------------------------------------------------------
public static Stack<int> Opdr3RandomNumbers(int lower, int upper, int count)
{
Stack<int> stack = new Stack<int>();
HashSet<int> voorgekomenGetallen = new HashSet<int>();

Random randomNumber = new Random();

while (stack.Count < count)
{
int number = randomNumber.Next(lower, upper);
bool hasOccurred = occurredNumbers.Contains(number);

while (hasOccurred) {
number = randomNumber.Next(lower, upper);
hasOccurred = occurredNumbers.Contains(number);
}

stack.Push(number);
occurredNumbers.Add(number);
}

return stack;
}
c#
/// ------------------------------------------------------------
/// <summary>
/// Creates a stack containing unique random numbers</summary>
/// <param name="lower">The lower bound for each number (inclusive)</param>
/// <param name="upper">The upper bound for each number (inclusive)</param>
/// <param name="count">The number of numbers</param>
/// <returns>The stack containing unique random numbers</returns>
/// ------------------------------------------------------------
public static Stack<int> Opdr3RandomNumbers(int lower, int upper, int count)
{
Stack<int> stack = new Stack<int>();
HashSet<int> voorgekomenGetallen = new HashSet<int>();

Random randomNumber = new Random();

while (stack.Count < count)
{
int number = randomNumber.Next(lower, upper);
bool hasOccurred = occurredNumbers.Contains(number);

while (hasOccurred) {
number = randomNumber.Next(lower, upper);
hasOccurred = occurredNumbers.Contains(number);
}

stack.Push(number);
occurredNumbers.Add(number);
}

return stack;
}
it cannot find a new number, this is the testcase it fails on: [TestCase(100, 100, 1)]
No description
17 Replies
canton7
canton710mo ago
Your message says [TestCase(100, 100, 1)], but your image says [TestCase(1, 100, 4)]?
Max
MaxOP10mo ago
yes, sorry copied the wrong one but it fails on both gets stuck in an infinite loop
canton7
canton710mo ago
Which of the two loops does it get stuck in?
Max
MaxOP10mo ago
the hasOccurred loop
canton7
canton710mo ago
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
canton7
canton710mo ago
Btw, you can simplify it to:
public static Stack<int> Opdr3RandomNumbers(int lower, int upper, int count)
{
Stack<int> stack = new Stack<int>();
HashSet<int> occurredNumbers = new HashSet<int>();

Random randomNumber = new Random();

while (stack.Count < count)
{
int number = randomNumber.Next(lower, upper);
if (occurredNumbers.Add(number))
{
stack.Push(number);
}
}

return stack;
}
public static Stack<int> Opdr3RandomNumbers(int lower, int upper, int count)
{
Stack<int> stack = new Stack<int>();
HashSet<int> occurredNumbers = new HashSet<int>();

Random randomNumber = new Random();

while (stack.Count < count)
{
int number = randomNumber.Next(lower, upper);
if (occurredNumbers.Add(number))
{
stack.Push(number);
}
}

return stack;
}
Or:
public static Stack<int> Opdr3RandomNumbers(int lower, int upper, int count)
{
Stack<int> stack = new Stack<int>();
HashSet<int> occurredNumbers = new HashSet<int>();

Random randomNumber = new Random();

while (stack.Count < count)
{
int number;
do
{
number = randomNumber.Next(lower, upper);
} while (!occurredNumbers.Add(number));

stack.Push(number);
}

return stack;
}
public static Stack<int> Opdr3RandomNumbers(int lower, int upper, int count)
{
Stack<int> stack = new Stack<int>();
HashSet<int> occurredNumbers = new HashSet<int>();

Random randomNumber = new Random();

while (stack.Count < count)
{
int number;
do
{
number = randomNumber.Next(lower, upper);
} while (!occurredNumbers.Add(number));

stack.Push(number);
}

return stack;
}
If you ask it for more values than as possible to generate it will get stuck, for sure: Opdr3RandomNumbers(1, 1, 2) will get caught. You probably want some guards to avoid that. Something like Opdr3RandomNumbers(1, 10000, 10000) might take a while to run, as it will need to generate a lot of random numbers before it happens to stumble across the 10000 that it needs
Max
MaxOP10mo ago
it gets stuck on Opdr3RandomNumbers(10, 15, 6); aswell
canton7
canton710mo ago
Yep, of course You've said "Give me 6 unique numbers between 10 and 15", and there are only 5 unique numbers between 10 and 15
Max
MaxOP10mo ago
so if i want to include 10 and 15 i should make the bounds 1 higher and lower?
canton7
canton710mo ago
With Random.Next, the lower bound is inclusive and the upper is exclusive
Max
MaxOP10mo ago
No description
Max
MaxOP10mo ago
this is what i am trying to reproduce
canton7
canton710mo ago
So if you want the upper bound to be inclusive, you'll need to add 1 to it, yes
Max
MaxOP10mo ago
okay thank you
canton7
canton710mo ago
This is how Python's random.sampledoes this FWIW: https://github.com/python/cpython/blob/main/Lib/random.py#L363 That can take what's effectively an ICollection<T> (so something which has a known length, but all items don't need to be listed in advance), and generate a random sample from it
GitHub
cpython/Lib/random.py at main · python/cpython
The Python programming language. Contribute to python/cpython development by creating an account on GitHub.
Max
MaxOP10mo ago
thank you, it finally works
Want results from more Discord servers?
Add your server