C
C#•15mo ago
jacks

need help getting rid of error

how do i make this error go away
No description
42 Replies
jacks
jacksOP•15mo ago
i also see another problem wait no
jacks
jacksOP•15mo ago
right here will it change the value of things.count while it's going thru the loop
No description
jacks
jacksOP•15mo ago
or does it only take in a value when first going into the loop
Patrick
Patrick•15mo ago
hover over the read squiggle, what does it say? 🙂
jacks
jacksOP•15mo ago
No description
jacks
jacksOP•15mo ago
not sure how to make it an int on declaration tho
Patrick
Patrick•15mo ago
there you go 🙂
jacks
jacksOP•15mo ago
i make it an int inside the loop so idk what to do
Patrick
Patrick•15mo ago
you don't capture the int you throw it away
mtreit
mtreit•15mo ago
What is the intent of using a random here?
jacks
jacksOP•15mo ago
shuffle the things
mtreit
mtreit•15mo ago
Pick a random item from things?
jacks
jacksOP•15mo ago
yes
mtreit
mtreit•15mo 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•15mo ago
i did it like this but now it's only returning 2 of the 3 options
No description
jacks
jacksOP•15mo ago
No description
mtreit
mtreit•15mo ago
So, things.Count is changing as your loop runs. That's a problem.
jacks
jacksOP•15mo 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•15mo ago
How do you think you might solve this? Hint: try using a different kind of loop.
jacks
jacksOP•15mo ago
No description
jacks
jacksOP•15mo ago
it's messing up because i'm messing with the things list during the loop
mtreit
mtreit•15mo ago
Yeah you can't modify something you are using foreach over.
jacks
jacksOP•15mo ago
hm
jacks
jacksOP•15mo ago
i was and it wasn't working
mtreit
mtreit•15mo 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.
🥄feeders help me learn faster !
It looks like Fisher-Yates.
jacks
jacksOP•15mo ago
yes
mtreit
mtreit•15mo ago
This isn't Fisher Yates Fisher Yates is in-place.
jacks
jacksOP•15mo ago
in this statement does it take the value of things only at the beginning
No description
jacks
jacksOP•15mo ago
or does it keep updating each time it goes through the loop
mtreit
mtreit•15mo ago
It keeps updating. That's the problem.
jacks
jacksOP•15mo ago
ohhhhh
jacks
jacksOP•15mo ago
i got it now
No description
jacks
jacksOP•15mo ago
tysm!!
mtreit
mtreit•15mo ago
I was actually suggesting this:
while (things.Count > 0)
while (things.Count > 0)
🙂
jacks
jacksOP•15mo ago
ohhhhhhhhhhh i never used a while loop before only do while and for i'll keep it in mind
mtreit
mtreit•15mo ago
while is the simplest loop.
jacks
jacksOP•15mo ago
i gotcha tysm for the help
mtreit
mtreit•15mo 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•15mo ago
that's super interesting while loop just never came to mind thank you for helping me realize
mtreit
mtreit•15mo ago
Sure thing.

Did you find this page helpful?