Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
2.50/5 (4 votes)
See more:
Hello.

I have a text box that can accept 5 chars, representing a numeric value from 00000 to 99999.

When the user enters a value, say for example, 12345, the following function is called:

if ((NumToValidate % 5) == 0)
        return VALIDATION_SUCCESS;


Where NumToValidate is the numerical form of the 5 char input.

The function checks that the inputed value, in this case 12345 is a multiple of 5, and if it is it returns as a success.

It works well, however, I have no idea how to create a function that can loop through generateing values that are a multiple of 5, from 00000 to 99999 (All possible values)

AND, if possible, to generate a random value and find its next multiple of 5.

e.g.

Rand value generated = 1834 now find what 5th digit, if any, produces a multiple of 5.

Thank you,
Stephen
Posted

Loop for generating vals divisible by 5

C#
for(int val = 0; val < MAX; val+=5)
{
   //Because we are adding 5 and starting at 0 val must be divisible by 5... unless 
   // you modify it inside the loop.
}


To generate a random value between 0 and 99999 use the random generator combined with modulus operator.

Random rand = new Random();
int val = random.Next(MIN, MAX);

//Now get to the 'next' divisible by 5
int leftOver = val % 5;
int newVal = val + leftOver; //Adding the leftover will result in divisible by 5
 
Share this answer
 
v3
Comments
stephen.darling 2-Aug-11 15:49pm    
Thank you, I think this is proberbly the best solution for my needs. Works for what I need at the moment, thank you again.
Stephen
[no name] 2-Aug-11 16:33pm    
You are welcome.
First we can generate a list with all mutiples of 5 between 0 and 100000 (not included).

C#
List<int> numbers = new List<int>;
for (int i = 0; i < 100000; i += 5)
{
    numbers.Add(i);
}


Then you can take a number at a random position and remove it from the list so that you will get each number only once.

C#
Random rand = new Random();

while (numbers.Count > 0)
{
    int randomIndex = rand.Next(numbers.Count);

    int randomNumber = numbers[randomIndex];

    // Could validate that the code works...
    System.Diagnostics.Debug.Assert((randomNumer % 5) == 0);

    numbers.RemoveAt(randomIndex);

    DoSomethingWithRandomMultipleOf5(randomNumbr);
}


If you don't want number to be returned only once, it is even easier.

C#
const int maxReturnedValue = 99995;
const int multipleOf = 5;
const int randLimit = (maxReturnedValue - 1) / multipleOf;

Random rand = new Random();

while (WantAnotherRandomNumber())
{
    int randomNumber = rand.Next(randLimit) * multipleOf;

    // Confirms that the code works. Could be useful when you
    // are not too sure that you code is correct.
    System.Diagnostics.Debug.Assert((randomNumer % 5) == 0);
    System.Diagnostics.Debug.Assert(randomNumer >= 0);
    System.Diagnostics.Debug.Assert(randomNumer <= 99995);

    DoSomethingWithRandomMultipleOf5(randomNumbr);
}
 
Share this answer
 
v2
Just for completeness, see the answer here[^] that I find most elegant, but David1987 was faster (a lot).

Edit: The link changed to another post[^].
 
Share this answer
 
v2
Your problem can be easily solved.

C#
protected void GenerateMultiples(int from, int to)
{
for (int i = from; i < to; i++)
{
     if(i % 5 == 0)
     {
        Console.WriteLine(i);
        i = i + 4; //Do this to make it faster.
     }
}
}


That is the best way I can think of.

For your second problem, remember that if a number is divisible by 5, it can only end in a 5 or a 0, so just add either to the end of your randomly generated number.
 
Share this answer
 
v4
Comments
CPallini 2-Aug-11 15:40pm    
This code doesn't generate all possible multiples.
DominicZA 2-Aug-11 15:53pm    
why doesn't this generate all possible multiples?
CPallini 2-Aug-11 16:11pm    
suppose from=2. When i=5 the program writes it and then sets i=25 (the 'Do this to make it faster'), missing the multiples 10,15,20.
DominicZA 2-Aug-11 16:32pm    
Oh wow...typo from my side...answered this from my phone so I didnt even realize :P Thanks for pointing this out.
Philippe Mori 2-Aug-11 19:09pm    
By doing i = i + 5 and i++, i will be incremented by 6. Then since you will align to a multiple of 5, it will incremented by 10 between each outputed number. Thus you will have either a series like 5, 15, 25, 35... or a series like 0, 10, 20, 30... depending on the initial number.
C#
Random random = new Random();
int num = random.Next(0, 1000);
for (int i = 0; i < 5; i++)
{
    if (num % 5 == 0)
    {
        MessageBox.Show(num.ToString());
    }
    num++;
}
 
Share this answer
 
v3
Comments
DominicZA 2-Aug-11 15:02pm    
Although not likely, this could end up becoming an infinite loop. Not a good solution.
luisnike19 2-Aug-11 15:45pm    
Sorry, answer updated.
luisnike19 2-Aug-11 15:46pm    
The next random num multiple of 5, between 0 and 1000

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900