Is there a function or algorithm for generating objects from a list randomly in c#, something likea random generation of questions for a test, if you get the idea?
Asked
Active
Viewed 102 times
-5
-
1Have you looked into [`Random`](http://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx)? – Travis J Dec 27 '13 at 16:12
-
this forum is for specific questions to programming problems. – T McKeown Dec 27 '13 at 16:13
-
"Generating" means creating to me. If they are in some list already, it's not about generating, only selecting them (at random or otherwise). – Konrad Morawski Dec 27 '13 at 16:13
-
yes, and I couldn't figure out which of these methods to use :/ – TheAdnan Dec 27 '13 at 16:15
-
try :http://stackoverflow.com/questions/9310111/fast-random-number-generator-in-c-sharp or http://stackoverflow.com/questions/1234094/how-can-i-generate-truly-not-pseudo-random-numbers-with-c – lsalamon Dec 27 '13 at 16:16
1 Answers
3
You need to store your objects, e.g. strings in a list, then select a random number as the list index. Something like:
Random rnd = new Random();
int idx = rnd.Next (0,myList.Length);
var randomObj = myList[idx];

Ali Alavi
- 2,367
- 2
- 18
- 22
-
-
-
-
Yes, you can use var and it will be casted to the type of the object. – Ali Alavi Dec 27 '13 at 16:19