I know that C # has a Random class and possibly several classes in LINQ, but if I were to write my own code to randomly select an item from a collection without using any built-in .NET objects, how can I do this?
I can’t cover up the logic required for this - how would I tell the system when to stop the iteration and select the current value - at random?
EDIT: This is a hypothetical question. This is not related to production coding. I'm just curious.
thanks
, - -, . , . , - 00 99, .
.
Random r = new Random(); int randomIndex = r.Next(0, myCollection.Size -1); var randomCollectionItem = myCollection[randomIndex];
, .
. , , , , - . .
, , .
. , , .
, . " ", :
rnd = seed; // some starting value rnd = (a * rnd + b) % c; // next value ...
a, b cthese . .
- "" - , , .. - , . , (IIRC , , , ).
- - - .
, , , . , - , , , .
int index = customRandomGenerator.Next(); var selection = items[index];
, ( ), :
int index = customRandomGenerator.Next(); Item selection = null; for (int i = 0; i < items.Length; i++) { if (i == index) { selection = items[i]; break; } }
" " .NET Framework System.Cryptography.RandomNumberGenerator - Reflector, , ? , , - - Random - Reflector.
, , , , , , , , , .
Source: https://habr.com/ru/post/1720610/More articles:Visual Studio Web Test vs. Selenium - seleniumCalling a remote COM + component using C # - .netSending authenticated user to WCF application - securityHTTP Get Content Type - httpEditing Wiki Page Award - designЕсть ли хорошая аналогия, объясняющая, почему использование веб-сайтов в отношении нестандартных тегов IE будет снижаться в последующие годы? - browserHow to create a multitasking portal application in ASP.NET MVC - architectureHow to get current user SID in VB6? - vb6What multithreading methods are available for iPhone OS, and what should I use? - multithreadingIs it possible to automate taking screenshots of part of a web page (for example, indicated by CSS selector or parent HTML element)? - javascriptAll Articles