C
C#•3y ago
TOLOXI

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
Angius
Angius•3y ago
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..?
TOLOXI
TOLOXIOP•3y ago
i need to convert it to an int first
Angius
Angius•3y ago
But why do you need it in the first place? The constructor of Random doesn't need any parameters
TOLOXI
TOLOXIOP•3y ago
im very new to coding so i thoguht that i needed to do it since it was an error i changed the code a bit
TOLOXI
TOLOXIOP•3y ago
to this
TOLOXI
TOLOXIOP•3y ago
but its still not picking a random name from the array
TOLOXI
TOLOXIOP•3y ago
i made this now
TOLOXI
TOLOXIOP•3y ago
but its giving me random numbers instead
Angius
Angius•3y ago
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 number
TOLOXI
TOLOXIOP•3y ago
yeah i changed it to C and its working now thanks a lot for the help
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit•3y ago
Can't control the seed 😦 At least as far as I can see
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit•3y ago
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
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit•3y ago
Yes, injecting dependencies via the constructor has many useful advantages.
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?