Enumerable.OrderBy help
I'm following a tutorial and they randomly get an element of an enumerable like this:
I'm a bit confused as to how this works. I looked up the docs for .OrderBy and kind of understand how it's usually meant to be used, but I don't see what throwing a random number in there does exactly.
15 Replies
It orders the elements by random number
What is that supposed to mean
Angius
REPL Result: Success
Result: List<ValueTuple<int, int>>
Compile: 373.080ms | Execution: 114.542ms | React with ❌ to remove this embed.
It does something like this under the hood
For each element, it generates a random number, and orders by that random number
Not how I would do getting a random item from an array, but it works
Hmm, interesting. How would you go about it?
if its an array, you can index a given location directly
so jsut randomize an index
^
OrderBy implies going through the entire sequence, generating a random number for each item, then sorting the list
wayyy more work for the computer
-1? upper bound is exclusive
Ah, derp, you're right
if you need multiple random items, there is also
Random.GetItems
with various overloadsWhy this instead of like
arr[Random.Range(0, arr.Length)]
?
Random
class does not have a static Range
method
https://learn.microsoft.com/en-us/dotnet/api/System.Random?view=net-9.0#methods
Is this a Unity thing or something?Oh yeah it is maybe that's where its coming from