C
C#ā€¢10mo ago
jacks

need help getting rid of error

how do i make this error go away
No description
42 Replies
jacks
jacksOPā€¢10mo ago
i also see another problem wait no
jacks
jacksOPā€¢10mo ago
right here will it change the value of things.count while it's going thru the loop
No description
jacks
jacksOPā€¢10mo ago
or does it only take in a value when first going into the loop
Patrick
Patrickā€¢10mo ago
hover over the read squiggle, what does it say? šŸ™‚
jacks
jacksOPā€¢10mo ago
No description
jacks
jacksOPā€¢10mo ago
not sure how to make it an int on declaration tho
Patrick
Patrickā€¢10mo ago
there you go šŸ™‚
jacks
jacksOPā€¢10mo ago
i make it an int inside the loop so idk what to do
Patrick
Patrickā€¢10mo ago
you don't capture the int you throw it away
mtreit
mtreitā€¢10mo ago
What is the intent of using a random here?
jacks
jacksOPā€¢10mo ago
shuffle the things
mtreit
mtreitā€¢10mo ago
Pick a random item from things?
jacks
jacksOPā€¢10mo ago
yes
mtreit
mtreitā€¢10mo ago
things[random.Next(0, things.Length)] You need to use random to produce an int in the range that you want. You do that by calling the Next method on random.
jacks
jacksOPā€¢10mo ago
i did it like this but now it's only returning 2 of the 3 options
No description
jacks
jacksOPā€¢10mo ago
No description
mtreit
mtreitā€¢10mo ago
So, things.Count is changing as your loop runs. That's a problem.
jacks
jacksOPā€¢10mo ago
i see but i don't want the same string to be duplicated so i'm removing it from the original list if i don't change the random max then it has a chance to land on an index that doesn't exist in the list
mtreit
mtreitā€¢10mo ago
How do you think you might solve this? Hint: try using a different kind of loop.
jacks
jacksOPā€¢10mo ago
No description
jacks
jacksOPā€¢10mo ago
it's messing up because i'm messing with the things list during the loop
mtreit
mtreitā€¢10mo ago
Yeah you can't modify something you are using foreach over.
jacks
jacksOPā€¢10mo ago
hm
i like chatgpt
i like chatgptā€¢10mo ago
use for.
jacks
jacksOPā€¢10mo ago
i was and it wasn't working
mtreit
mtreitā€¢10mo ago
Let's describe the algorithm in plain english and you can translate it to code: To shuffle the list: while the list of things is non-empty, pick a random number between 0 and how many things the list contains. Add the item at that index in things to the end of the result list. Now remove it from things.
i like chatgpt
i like chatgptā€¢10mo ago
It looks like Fisher-Yates.
jacks
jacksOPā€¢10mo ago
yes
mtreit
mtreitā€¢10mo ago
This isn't Fisher Yates Fisher Yates is in-place.
jacks
jacksOPā€¢10mo ago
in this statement does it take the value of things only at the beginning
No description
jacks
jacksOPā€¢10mo ago
or does it keep updating each time it goes through the loop
mtreit
mtreitā€¢10mo ago
It keeps updating. That's the problem.
jacks
jacksOPā€¢10mo ago
ohhhhh
jacks
jacksOPā€¢10mo ago
i got it now
No description
jacks
jacksOPā€¢10mo ago
tysm!!
mtreit
mtreitā€¢10mo ago
I was actually suggesting this:
while (things.Count > 0)
while (things.Count > 0)
šŸ™‚
jacks
jacksOPā€¢10mo ago
ohhhhhhhhhhh i never used a while loop before only do while and for i'll keep it in mind
mtreit
mtreitā€¢10mo ago
while is the simplest loop.
jacks
jacksOPā€¢10mo ago
i gotcha tysm for the help
mtreit
mtreitā€¢10mo ago
Every other loop is basically syntactic sugar over a while loop.
int x = 0;
do
{
Console.WriteLine(x);
}
while (x-- > 0);
int x = 0;
do
{
Console.WriteLine(x);
}
while (x-- > 0);
Like this code is transformed by the compiler to this:
int value = 0;
while (true)
{
Console.WriteLine(value);
if (value-- <= 0)
{
break;
}
}
int value = 0;
while (true)
{
Console.WriteLine(value);
if (value-- <= 0)
{
break;
}
}
jacks
jacksOPā€¢10mo ago
that's super interesting while loop just never came to mind thank you for helping me realize
mtreit
mtreitā€¢10mo ago
Sure thing.
Want results from more Discord servers?
Add your server