By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,543 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,543 IT Pros & Developers. It's quick & easy.

How to randomize 5 Numbers I input?

P: 97
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:
Expand|Select|Wrap|Line Numbers
  1. Random random = new Random();
  2. return random.Next(min, max);
  3.  
But how do I randomize those 5 numbers I already have?

I want something like
Expand|Select|Wrap|Line Numbers
  1. int returnValue = RandomNumber(50, 578, 31, 43, 78);
Any help will be appreciated. Thanks!
Sep 18 '08 #1
Share this Question
Share on Google+
5 Replies


balabaster
Expert 100+
P: 797
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.
Expand|Select|Wrap|Line Numbers
  1. int[5] vals;
  2. vals[0] = First Number
  3. vals[1] = Second Number
  4. ...
  5. vals[5] = Last Number
  6.  
  7. int chosen = random.Next(0,5);
  8. Return vals[chosen];
Sep 18 '08 #2

Expert 100+
P: 427
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.
Sep 18 '08 #3

balabaster
Expert 100+
P: 797
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.
Expand|Select|Wrap|Line Numbers
  1. int[5] vals;
  2. vals[0] = First Number
  3. vals[1] = Second Number
  4. ...
  5. vals[5] = Last Number
  6.  
  7. int chosen = random.Next(0,5);
  8. 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:

Expand|Select|Wrap|Line Numbers
  1. list<int> RandomNumbers = new list<int>;
  2. int LoopCount = random.Next(1, 100); //we're generating somewhere between 1 and 100 integers
  3. int Low = random.Next(1, 50); //The low end of the spectrum will always be between 1 and 50
  4. int High = random.Next(51,100); //The high end will always be between 51 and 100
  5. for(i = 0; i < LoopCount; i++){
  6.   RandomNumbers.add(random.Next(Low, High));
  7. }
  8. //Now we need to pick one...
  9. Low = 0;
  10. High = RandomNumbers.Length - 1;
  11. int Selected = random.Next(Low, High);
  12. 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...
Sep 18 '08 #4

P: 97
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:
Expand|Select|Wrap|Line Numbers
  1. // Declare an array 
  2.                 String[] drinkList = new String[5];
  3.  
  4.                 // Place some items in it 
  5.                 drinkList[0] = txt1.Text;
  6.                 drinkList[1] = txt2.Text;
  7.                 drinkList[2] = txt3.Text;
  8.                 drinkList[3] = txt4.Text;
  9.                 drinkList[4] = txt5.Text;
  10.  
  11.  
  12.                 int returnValue = RandomNumber(0,4);
  13.                 textBox12.Text = drinkList[returnValue];
  14.  
Really appreciate it. Cheers!
Sep 18 '08 #5

balabaster
Expert 100+
P: 797
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:
Expand|Select|Wrap|Line Numbers
  1. // Declare an array 
  2.                 String[] drinkList = new String[5];
  3.  
  4.                 // Place some items in it 
  5.                 drinkList[0] = txt1.Text;
  6.                 drinkList[1] = txt2.Text;
  7.                 drinkList[2] = txt3.Text;
  8.                 drinkList[3] = txt4.Text;
  9.                 drinkList[4] = txt5.Text;
  10.  
  11.  
  12.                 int returnValue = RandomNumber(0,4);
  13.                 textBox12.Text = drinkList[returnValue];
  14.  
Really appreciate it. Cheers!
Glad our input was useful :o)
Sep 18 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.