How to randomize 5 Numbers I input?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gggram2000
    New Member
    • Nov 2007
    • 97

    How to randomize 5 Numbers I input?

    Hi,
    I'ved spent two full days trying to find a solution, I can randomize numbers between two ranges and it works fine, But my problem is when i want to randomize five numbers that I got. eg.

    I randomize 1-100 and I get 50.
    I randomize 1-900 on another input field and I get 578.
    I randomize 2-56 and get 31.
    " " and get 43.
    " " and get 78.

    So I have five numbers: 50, 578, 31, 43 and 78.

    I want to randomize those 5 numbers and get one to display.

    This is to randomize Between two numbers:
    Code:
    Random random = new Random();
    return random.Next(min, max);
    But how do I randomize those 5 numbers I already have?

    I want something like
    Code:
    int returnValue = RandomNumber(50, 578, 31, 43, 78);
    Any help will be appreciated. Thanks!
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    Dump them into an array of 6 values, do another randomize between 0 and 5 and spit out the value in whatever array position is returned...

    You've already got the code to generate a random number between a range, so you can reuse your existing code.
    Code:
    int[5] vals;
    vals[0] = First Number
    vals[1] = Second Number
    ...
    vals[5] = Last Number
    
    int chosen = random.Next(0,5);
    Return vals[chosen];

    Comment

    • cloud255
      Recognized Expert Contributor
      • Jun 2008
      • 427

      #3
      Well the randomize() function works on the current system time and has an upper and lower bound.

      I dont believe that there is any built in function to perfrom what you want.

      What you may try is to add your 5 numbers and then perform a randomize on the sum.

      Or you could ad the the first 2 randomize on the sum, add this result to the 3rd number randomize on the sum and repeat.

      Or you could put the number in an array where the array element of each number comes from a randomize with an upper bound of 5 and then perform the above actions.

      Or you could create a function which randomizes based on 2 numbers, this will be the first and second numbers from your 5 random numbers, the result of this (that is the first second generation random number) is then stored ( for later use) and also used with the 3rd first generation number to generate your second second generation random number.
      Repeat until all five first generation numbers have been used. you now have 4 second generation random numbbers, repeat to create 3 third generation numbers, 2 fourth generation numbers and those 2 numbers will then provide a single fifth generation random number.

      hope this helps, please do not hesitate to post if i have missed your goal or if one of the above approaches are unclear.

      Good luck.

      PS there are a few other things you could do to come up with some random results, the above list does not include all possibilities.

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Originally posted by balabaster
        Dump them into an array of 6 values, do another randomize between 0 and 5 and spit out the value in whatever array position is returned...

        You've already got the code to generate a random number between a range, so you can reuse your existing code.
        Code:
        int[5] vals;
        vals[0] = First Number
        vals[1] = Second Number
        ...
        vals[5] = Last Number
        
        int chosen = random.Next(0,5);
        Return vals[chosen];
        If you've got a dynamically sized array, you'd just use vals.length as the second parameter of random.Next...

        So lets say we generate a random number of random numbers, we pick some arbitrary number out of the air and run a loop that amount of times. Each time, we add some random number to a list. Once we've run the loop, we run the random number generator to figure out which number to pick:

        Code:
        list<int> RandomNumbers = new list<int>;
        int LoopCount = random.Next(1, 100); //we're generating somewhere between 1 and 100 integers
        int Low = random.Next(1, 50); //The low end of the spectrum will always be between 1 and 50
        int High = random.Next(51,100); //The high end will always be between 51 and 100
        for(i = 0; i < LoopCount; i++){
          RandomNumbers.add(random.Next(Low, High));
        }
        //Now we need to pick one...
        Low = 0;
        High = RandomNumbers.Length - 1;
        int Selected = random.Next(Low, High);
        return RandomNumbers(Selected);
        There, that will produce a random length list of random numbers selected from random ranges and then give you a single random selection from that randomly generated list.

        Not sure why all that is necessary though...

        Comment

        • gggram2000
          New Member
          • Nov 2007
          • 97

          #5
          Hey
          Thanks a lot guys, all ur input really helped me. Was getting a headache for a minute and was generating a number after some equations but thanks to ur help I can randomize the five numbers fairly easy.
          This is my code to get desired number:
          Code:
          // Declare an array 
                          String[] drinkList = new String[5];
          
                          // Place some items in it 
                          drinkList[0] = txt1.Text;
                          drinkList[1] = txt2.Text;
                          drinkList[2] = txt3.Text;
                          drinkList[3] = txt4.Text;
                          drinkList[4] = txt5.Text;
          
          
                          int returnValue = RandomNumber(0,4);
                          textBox12.Text = drinkList[returnValue];
          Really appreciate it. Cheers!

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            Originally posted by gggram2000
            Hey
            Thanks a lot guys, all ur input really helped me. Was getting a headache for a minute and was generating a number after some equations but thanks to ur help I can randomize the five numbers fairly easy.
            This is my code to get desired number:
            Code:
            // Declare an array 
                            String[] drinkList = new String[5];
            
                            // Place some items in it 
                            drinkList[0] = txt1.Text;
                            drinkList[1] = txt2.Text;
                            drinkList[2] = txt3.Text;
                            drinkList[3] = txt4.Text;
                            drinkList[4] = txt5.Text;
            
            
                            int returnValue = RandomNumber(0,4);
                            textBox12.Text = drinkList[returnValue];
            Really appreciate it. Cheers!
            Glad our input was useful :o)

            Comment

            Working...