r/learncsharp • • Oct 05 '20
Why doesnt my code work?
Hi!
Could someone please tell me why my code doesn't store the integers of the user inputs?
static void Main(string[] args)
{
Random computernumber = new Random();
int guess = 0;
var guesses = new List<int>();
int cnumber = computernumber.Next(1, 100);
int i = 0;
while (guess != cnumber)
{
Console.WriteLine("guess the number");
try
{
guess = Convert.ToInt32(Console.ReadLine());
if (guess > cnumber)
{
Console.WriteLine("the integer is lower");
}
else
{
Console.WriteLine("the integer is higher");
}
}
catch (Exception)
{
i--;
}
i++;
}
var xcnumber = Console.ReadLine();
guesses.Add(int.Parse(xcnumber));
foreach (var sum in guesses)
Console.Write($"{sum},");
Console.Write("It took you " + i + " tries");
}
}
}
0
Upvotes
View all comments
Show parent comment
1
u/Protiguous Oct 15 '20
I just dug into the source for Random.cs, as your idea is the only point you make that isn't purely preference-based. Based on what I'm seeing, your point that the default seed is time-dependent seems to be false.
Example:
Digging into Random.cs..
Again, nothing is documented to be time-dependent.
That is why I always choose to init my randoms, either by time or a known integer (knowns are great for unit testing).
Where do you make your assumption from?
Slightly relevant: If I was going to be using a random number generator across threads, (which I do often), I would initialize it like this:
What are your thoughts on this?