.NET 8 Adds Shuffle to Random for Arrays and Spans
ฝัง
- เผยแพร่เมื่อ 26 พ.ย. 2023
- In .NET 8, we now have a method to randomly shuffle arrays and spans. Let's look at it in this episode of 10 minute training.
Full Training Courses: IAmTimCorey.com
Source Code: leadmagnets.app/?Resource=Ran...
Mailing List: signup.iamtimcorey.com/
crazy they didn't add a overload for a list considering it is backed by an array anyways, oh well. thanks for the video Tim.
That would be nice.
I think it probably has something to do with garbage collection. both arrays and span are stack allocated.
maybe the shuffling would mess up the reference tree if it was allocated on the heap.
Good content and you've got to keep up with the Jones.
Thanks.
Howdy Tim! I was wondering if you were planning on updating the course website to use the new blazor or if you were going to continue without it. I ask since I have watched the why not blazor video on the iamtimcorey website course and it seems the new blazor might work out now.
I’m debating if I redo it using Blazor or not. There are compelling reasons both ways. I’m going to take some time to consider the options. After that, I’ll publish something about the process at least.
I JUST needed something like this in a little utility that I'm using to learn the guitar fretboard... notes, chords, scales, and arpeggios - that sort of thing - and simply using .NET Random.Next() wasn't creating a good distribution (or maybe it was TOO good! Being asked to "Find the C on the A string" 4 times in a row... not ideal!), so I took to the interwebs to see what some options would be. This led me to learning about Fisher-Yates, Durstenfeld, and a reminder that one of these days, I should really make a better effort to get further than the first 50 pages of The Art of Computer Programming, because Knuth has a little something to say about algorithms! Anyways - creative use of a good ol' Shuffle provided a useful distribution of "random" objects.
Thanks for sharing!
how to create a new shuffled list and keep the original one in the same order?
Personally I would clone the existing list and shuffle the new one.
That's at least how I do it in card games for the deck.
Mr Corey, do you have plan for making tutorial about publishing apps in MS Store?
Not at this time. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
do you have any video on ABP Framework and .net Zero?
Nope, not yet. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
There's no such thing as random when dealing with binary, there's always a seed. I'm interested what seed was used in this function. (I'm not suggesting you left anything out.) But there must be a seed whether it be time, some screwy calculation based on time, where the processor is in cycle count, etc... but there is no true random in computing, just a random you can't figure out in that carbon based thing in your head..... but this was cool to learn about and keep up the awesome content!
There's no such thing as generated random numbers. That's why Cloudflare uses 100 lava lamps to generate random numbers. However, for most cases, the Random library in C# works just fine (just not for cryptography). In .NET 6+, the Random library was upgraded to include the static Shared option. This is a thread-safe way to generate pseudo-random numbers in C# now. We don't get to set the seed value, though. If you want to do that, you would instantiate your own version of the Random class instead. As for what specific seed value it uses to start out, I'm not sure.