how do i pick a random value from an array
im coding a simple random name picker but there something wrong with the code
17 Replies
You generate a random value between 0 and array length
Then you use it to grab the value from said array
Also... what's going on with that
name1
and passing that to the random..?i need to convert it to an int first
But why do you need it in the first place?
The constructor of
Random
doesn't need any parametersim very new to coding so i thoguht that i needed to do it since it was an error
i changed the code a bit
to this
but its still not picking a random name from the array
i made this now
but its giving me random numbers instead
Well, you need to actually pick that name
And then display it
Right now, your random number is
r
And the randomly-selected name is c
You're printing r
, so you'll see the random numberyeah i changed it to C and its working now thanks a lot for the help
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Can't control the seed 😦
At least as far as I can see
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Isn't kind of the whole point of Random.Shared is that it's thread safe? If you always inject an instance like you likely don't have a problem with sharing the instance in the first place.
I take your point though.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Yes, injecting dependencies via the constructor has many useful advantages.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View