tags:

views:

113

answers:

7

Hi,

I know C# has the Random class and probably a few classes in LINQ to do this, but if I was to write my own code to randomly select an item from a collection without using any built in .NET objects, how would this be done?

I can't seem to nail the logic required for this - how would I tell the system when to stop an iteration and select the current value - at random?

EDIT: This is a hypothetical question. This is not related to a production coding matter. I am just curious.

Thanks

A: 

You need to use a seed, something semi random provided by the computer itself. Maybe use very fine resolution time and use the last couple microseconds when the method is called. That should be random enough to generate anything from 00 to 99, you can then go from there.

Matt
This is completely unnecessary; C#'s Random class already does this or something similar. If you were to write code to this effect and then feed the seed into the Random class, your code would be completely redundant.
Timwi
I know .NET has a Random class but he seemed to ask how he could write his own random class...
Matt
A: 

It sounds like your problem isn't in calculating a random number, but in how to use that random number to select an item from a list. Assuming you can create a random number somehow, all you need to do is use it as the argument to the list's indexer.

int index = customRandomGenerator.Next();
var selection = items[index];

Assuming that your presupposition about having to iterate through the list is correct (or the collection doesn't have an indexer) then you could do:

int index = customRandomGenerator.Next();
Item selection = null;
for (int i = 0; i < items.Length; i++) 
{
  if (i == index)
  {
    selection = items[i];
    break;
  }
}
gWiz
A: 

The only true "cryptographically strong" random number generator in the .Net Framework is in System.Cryptography.RandomNumberGenerator - run this through Reflector to see what is does? Looking at your problem you would need a to know the Count of the collection otherwise you may never retrieve an item - you would need to specify a start and end value to draw random numbers from - the Random class would work best - pop it through Reflector.

koosk
+3  A: 

Selecting a random element from a collection can be done as follows.

Random r = new Random();
int randomIndex = r.Next(0, myCollection.Size -1);
var randomCollectionItem = myCollection[randomIndex];

Unless you have a VERY good reason, writing your own random generator is not necessary.

Rik
This is exactly how it should be done.
Timwi
A: 

Well, I never thought about implementing that myself as it seems like reinventing the wheel but you may have a look on this wikipedia article, hope it helps you do what you want

Random Number Generator

Waleed Eissa
+1  A: 

There are many pseudo-random number generators. They aren't truly random, but they come at different quality, distinguished by their statistical and sequential properties and what purpose they are applicable for.

It very much depends on "how random you need it". If it just needs to "look random to a human", simple generators look like that:

rnd = seed; // some starting value 
rnd = (a * rnd + b)  % c;  // next value
...

For well chosen values of a, b, and cthese generators are ok for simple statistical tests. A detailed discussion and common values for these you find here.


One interesting approach is to collect as much "external" data as possible - like time between keypresses, mouse movements, duration of disk reads etc. -, and use an algorithm that accumulates randomness while discarding dependency. That is mathematically tricky though (IIRC not long ago a critical attack surfaced based on one of these not being as random as thought).


Only a very few special applications use a truly random external hardware source - anything between a open-imput amplifier and radioactive decay.

peterchen
+2  A: 

My advice to you is DON'T DO IT. Whatever reason you think you may have for not wanting to use the built-in library, I am pretty sure you misunderstood something. Please go back to the drawing board.

All of the advice above is technically accurate, but is kind of like giving a chemistry textbook to someone who wants to refine his own oil to use in his car.

leo grrr