none
C# Random Sorting

    Question

  • Hi!
    I'm have been searching the internet to a thing that I'm trying to implement and still haven't found the answer that I was looking for.
    What I want basically is from a define set of random values, pick each ones of those values randomly, not according to there position in the array.
    For example: The algorithm will know that I have  a set of values { 3,78,100,23,12,99,6 } from this values he will pick one at a time randomly, is this possible to be implemented, is kind of a lottery algorithm but the values are not in a sequence, is this  possible to do in C#?. Any help pseudo code, examples, what ever would be appreciated. Thanks.
    I don't know if this is the right forum to post this.
    Tuesday, July 22, 2008 5:13 PM

Answers

  • You should have come here instead of searching in the vast Internet desert:) MS has everything for a man like you. 

    http://msdn.microsoft.com/en-us/library/system.random.aspx

    Random Class.

    It is vey easy to implement what you are trying to do. No problem. Will take you perhaps 15 min of coding.
    AlexB
    • Marked as answer by jack 321 Friday, July 25, 2008 6:42 AM
    Tuesday, July 22, 2008 5:30 PM
  • I know you said "not according to there position in the array" but the rest of your post makes it sound like that's exactly what you want.  Here's code.

                int[] values = { 3,78,100,23,12,99,6 }; 
                Random rnd = new Random(DateTime.Now.Millisecond); 
                Console.WriteLine(values[rnd.Next(0, 7)].ToString()); 

    This will limit the return of your random to only the set of data you specified.  Each run will return a random integer, either 3, 78, 100, 23, 12, 99, or 6.  That sounds like what you want, however, it is using a random position in the array...so...let me know. =)

    • Marked as answer by jack 321 Friday, July 25, 2008 6:42 AM
    Tuesday, July 22, 2008 5:52 PM

All replies

  • You should have come here instead of searching in the vast Internet desert:) MS has everything for a man like you. 

    http://msdn.microsoft.com/en-us/library/system.random.aspx

    Random Class.

    It is vey easy to implement what you are trying to do. No problem. Will take you perhaps 15 min of coding.
    AlexB
    • Marked as answer by jack 321 Friday, July 25, 2008 6:42 AM
    Tuesday, July 22, 2008 5:30 PM
  • Sure I already know about the random class but this class doesn't have a method to set a collection of values to random from, only a interval, and if the interval is between 9 and 150 because the lower and upper values are those then I have a lot results that might be the ones that I want, and I don't have any clue how to implement this, a few hints would help. Thanks for the reply.
    Tuesday, July 22, 2008 5:36 PM
  • I know you said "not according to there position in the array" but the rest of your post makes it sound like that's exactly what you want.  Here's code.

                int[] values = { 3,78,100,23,12,99,6 }; 
                Random rnd = new Random(DateTime.Now.Millisecond); 
                Console.WriteLine(values[rnd.Next(0, 7)].ToString()); 

    This will limit the return of your random to only the set of data you specified.  Each run will return a random integer, either 3, 78, 100, 23, 12, 99, or 6.  That sounds like what you want, however, it is using a random position in the array...so...let me know. =)

    • Marked as answer by jack 321 Friday, July 25, 2008 6:42 AM
    Tuesday, July 22, 2008 5:52 PM
  • Thanks for the help, after seeing all the information that everyone posted I realized that is the only way to do it, I was thinking in a weird wait.... so thanks once more for all the help.
    Tuesday, July 22, 2008 6:54 PM
  • Bite your tongue!  We never say "the only way to do it" here!  There's always 53 ways to do it, 50 of them bad, 2 of them good, and 1 of them that only Peter Ritchie or Hans Passant has ever heard of.=)

    Tuesday, July 22, 2008 6:59 PM
  • nikeman_1479 said:

    Sure I already know about the random class but this class doesn't have a method to set a collection of values to random from, only a interval, and if the interval is between 9 and 150 because the lower and upper values are those then I have a lot results that might be the ones that I want, and I don't have any clue how to implement this, a few hints would help. Thanks for the reply.

    You harmonize, my dear. There is a simple, simple ratio to convert one interval to another. It is a simple arithmetic. I assumed you know the basics. Nobody will desiggn a Random Number Generator specifically for interval 9,150. What if tomorrow you will come up and say: now I want to have a RNG for 9,149 interval?

    This is simply pathetic. You should retake your HSE diploma exam.


    AlexB
    Tuesday, July 22, 2008 8:04 PM