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