Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Forms WinForm
Hello,
What to do if I want to generate random numbers to more variables but some of them will have generated value except value which is saved in some other variables? It may be hard to understand what I want Big Grin | :-D so I will show you some code (which is wrong but it can explain better what I mean).
 
int variable_1 = rand.Next(1,14)
int variable_2 = rand.Next(1,14.Except(variable_1)) //So what I want is to save value to variable_2 which minimum is 1 and maximum is 14 but the value of variable_2 can't be equal to value in variable_1
 
The sollution which will work for even more variables would be best Smile | :) . Can you please help me? I tried to think about some algorithms myself and tried google and MSDN and nothing helped me. Thanks for any kind of help!
Posted 19-Apr-12 11:31am
Comments
Peter_in_2780 at 19-Apr-12 18:03pm
   
Is your problem the same as shuffling a deck of cards and dealing one at a time? If so, do just that. Make an array of the integers you can choose from, then use a random number generator to swap pairs many times. Then just take the numbers in order.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I don't know if there is a direct way to do that at once, but you could do the generation of the second random number inside a loop that breaks when it is different of the previous one.
 
I mean something like (getting your code as example):
 
int variable_1;
int variable_2;
variable_1 = rand.Next(1,14)
do
{
   variable_2 = rand.Next(1,14)
} while (variable_2 == variable_1) // If the first try gets the same value it repeats until it generate one that is not equal
 
But this solution can only be taken into consideration if the number of variables to check before accepting the new one is not big, so the conditions don't get too much. Using a recursive function that checks the previous ones and recalls itself if not matching would be another possibility.
 
Problem with that is: the bigger the number of variables you want to create, the bigger time it gets to find a correct value for the next.
 
how many variables are you speaking about? And the range for the random numbers? Must all variables have "unique" value?
If there are many variables and all of them must have a unique value you could do another thing, like creating an array to hold the values from 1 to range_limit, sorting them randomally and getting them from there.
  Permalink  
v3
Comments
LosEagle at 19-Apr-12 18:20pm
   
Well it seems I found sollution. Thank you :)
Nelek at 19-Apr-12 19:19pm
   
You are welcome
Wes Aday at 19-Apr-12 23:18pm
   
5
Nelek at 20-Apr-12 16:35pm
   
Thank you Wes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 515
1 Maciej Los 340
2 Richard MacCutchan 265
3 BillWoodruff 225
4 Mathew Soji 155
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.1 | Last Updated 19 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100