# Thread: Calculate sum of values

1. ## Calculate sum of values

Hi all,

First post. Be gentle!

I'm trying to set up a system that will generate a random 6 digit number and once I press another button (Change Keys) it will give me a list of 2 different numbers that go in to each unit.

I have the random number generator figured out. It's the next bit I'm stuck on.

In the RNG I wanted to eliminate the possibility of it returning a 0, only wanting values 1 to 9 so each of them are a separate label. I wasn't sure of an easier way to do it.

For these purposes please ignore the smaller set of numbers below, they're for a different reason.

Example:

and here is the code:

Code:
```
private void generatorButton_Click(object sender, EventArgs e)
{
Random generator = new Random();
int newNumber1 = generator.Next(1, 4);
int newNumber2 = generator.Next(1, 7);
int newNumber3 = generator.Next(1, 7);
int newNumber4 = generator.Next(1, 7);
int newNumber5 = generator.Next(1, 7);
int newNumber6 = generator.Next(1, 7);
outputLabel1.Text = newNumber1.ToString();
outputLabel2.Text = newNumber2.ToString();
outputLabel3.Text = newNumber3.ToString();
outputLabel4.Text = newNumber4.ToString();
outputLabel5.Text = newNumber5.ToString();
outputLabel6.Text = newNumber6.ToString();
outputLabel7.Text = newNumber7.ToString();
}```
In the example provided I want a report published once I press the 'Change Keys' button to provide me a list like so:

Bottom: 1 4 3 2 4 2
Master: 2 x 3 4 x x

Bottom: 3 2 2 6 1 2
Master: x 2 4 x 3 2

Bottom: 3 3 1 2 3 1
Master x 1 5 4 1 1

This generates a new code, keeping some of the values the same, ideally based on a checkbox selecting to change 2/3 of the numbers. But for the ones it has changed I want below it to show me what number I can add to take it to the original number.

I then want to be able to print that report.

I hope this makes sense!

EDIT:

Just to add, the new number can not be higher than the original.

2. ## Re: Calculate sum of values

Since your random selection of the initial numbers can be 1, you can't select a number less than 1 for that spot.
Also, I don't understand what the following sentence means.
In the RNG I wanted to eliminate the possibility of it returning a 0, only wanting values 1 to 9 so each of them are a separate label. I wasn't sure of an easier way to do it.
You mention 1 to 9, but you initial numbers are randomly chosen, and the first can only be 1,2 or 3 so there isn't much wiggle room there.
The rest of the digits can only be 1 to 6, so using 6, 7, 8 or 9 seems to not be applicable so I don't understand what the sentence is trying to say.

Since you have picked a value, and you want to pick a new value less than that, you should just use the original value as your maximum value in a Random.Next function call.

Since you want a separate button to generate the "keys", then I would move the declaration of the numbers out to the form class scope so they would be accessible to code written in each buttons event handler.

I think for flexibility in the code, instead of using six separate variables to hold the numbers, you should use an array that you can index.
That will make it easier to choose which numbers you want to operate on, and which you want to leave the same, i.e. you could choose an index randomly, i.e. from 0 to 5 for six digits, and then change digit[3] if you want to change the 4th digit, rather than have to deal with a hardcoded variable name that represents the fourth digit.

3. ## Re: Calculate sum of values

I'm trying to create a Master Key Pinning System. I'm not sure if you know how they work in particular but it's similar to how I explained.

Our Master Key would have a code of for example 334214, those indicate pin sizes for the lock. Now we know that a key cut to that size will open that lock, we can also cut a key to 132214 and pin the lock to:

2 2
132214

The bottom row is our pins that allow the second key to open the lock, above are 2 master wafers on size 2. This allows the lock to open to key combinations 334214, 132214, 332214, 134214 etc.

We don't want any 0 values in our code as that's not a figure we cut the keys to, our minimum cut is 1. We don't want any cuts deeper than a 4 for the first cut as it can make it difficult to get the key in. When I had the RNG generate a long string such as a value between 1 and 666667 I got codes such as 406810 so the only way I knew how (new to coding) was to create 6 separate RNG's only generating values 1 to 9 in order to eliminate any 0's.

Ideally as well we don't want to have a master wafer on the first cut but are fine to add them after that, again it can make the key difficult to get in. Like you said though, for 1 cuts we clearly can't have wafer pins added so I need a way to stop the code from trying to add wafers to that.

My reason for wanting a second form to open with all the 'change keys' is because it's not always needed. Sometimes we only need 1 code for a new lock without any master keys, so the first code will work fine without having to open the extras.

I hope all that makes sense!

Originally Posted by passel
Since your random selection of the initial numbers can be 1, you can't select a number less than 1 for that spot.
Also, I don't understand what the following sentence means.

You mention 1 to 9, but you initial numbers are randomly chosen, and the first can only be 1,2 or 3 so there isn't much wiggle room there.
The rest of the digits can only be 1 to 6, so using 6, 7, 8 or 9 seems to not be applicable so I don't understand what the sentence is trying to say..
You're right, there was an error in my code from previous testing, meant to generate numbers up to 9, not 6.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured