Click here to Skip to main content
15,891,981 members
Home / Discussions / C#
   

C#

 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
stephen.darling22-Aug-11 23:19
stephen.darling22-Aug-11 23:19 
AnswerRe: How do I generate a number divisable by 5, and check it? Pin
Tom Chantler23-Aug-11 1:15
professionalTom Chantler23-Aug-11 1:15 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
Member 462161222-Sep-11 4:53
Member 462161222-Sep-11 4:53 
AnswerRe: How do I generate a number divisable by 5, and check it? Pin
Paulo_JCG23-Aug-11 2:09
professionalPaulo_JCG23-Aug-11 2:09 
AnswerRe: How do I generate a number divisable by 5, and check it? Pin
Member 798954823-Aug-11 4:07
Member 798954823-Aug-11 4:07 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
Michael A. Cochran23-Aug-11 4:41
Michael A. Cochran23-Aug-11 4:41 
AnswerRe: How do I generate a number divisable by 5, and check it? Pin
lucien6423-Aug-11 4:56
professionallucien6423-Aug-11 4:56 
AnswerRe: How do I generate a number divisable by 5, and check it? [modified] PinPopular
Michael A. Cochran23-Aug-11 4:56
Michael A. Cochran23-Aug-11 4:56 
XML
// To generate as random numbers as possible, this variable must only
// be initialized once and then reused as much as possible.
Random rand = new Random();
/// <summary>
/// Generates a random 5 digit number that is always divisible by 5.
/// </summary>
/// <returns>Returns a random integer 5 digits long that is evenly divisible by 5.</returns>
private int GetDivBy5()
{

    // rnd.Next(minValue, maxValue) returns a random between minValue
    // and maxValue, inclusive. Use min/max values of 1000 and 9999 to
    // yield a random 4 digit number between 1000 and 9999 then multiply
    // by 10 to get a 5 digit number.  This number will always be
    // divisible by 5 but will always end in 0.
    int divBy5 = rand.Next(1000, 9999) * 10;

    // To get more random and double the possible result set, randomly
    // add on 5.  This will yield the total possible set of integers
    // between 10000 and 99995 that are divisible by 5.
    if (rand.NextDouble() < 0.5)
        return divBy5;
    else
        return divBy5 + 5;
}


Stephen, you mention you are a coding noobie. So, a short word about random number generators - they're not really very random.


All they do is take a seed (beginning) value and run it through a mathmatical algorithm that generates a new number. If you use the same seed value every time, you will get the same "random" number every time. Most random number generators can either self-seed - generally using the current clock value - or you can pass them a seed value when you initialize them. This is why you only want to initialize your random number generator once and reuse it over and over.

Once you get past that, the rest is pretty easy. Just use the math several others have already provided. See the comments in the code...

<edit>
Um, just looked at the thread in the other forum where you defined your requirements a little better. It seems you would like to include 0-9999 in your result set too. You can do this easily enough by changing the min value passed to rand.Next(minValue, maxValue) to 0 instead of 1000. This last bit, is a bit tricky in that in programming terms "00005" is actually not a number but is really a string. To get the leading zeros, you need to convert your final value to a string and use a formatting mask to append the leading zeros. This also assumes "00000" is valid. If it's not, test for it with an "if" and call GetDivBy5 again.

So, to get a string with leading zeros use the following;
C#
private void button1_Click(object sender, EventArgs e)
{
    this.textBox1.Text = this.GetDivBy5().ToString("00000");

}

</edit>

modified on Tuesday, August 23, 2011 11:03 AM

GeneralRe: How do I generate a number divisable by 5, and check it? Pin
stephen.darling23-Aug-11 5:38
stephen.darling23-Aug-11 5:38 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
Chris Maunder23-Aug-11 6:24
cofounderChris Maunder23-Aug-11 6:24 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
stephen.darling23-Aug-11 6:56
stephen.darling23-Aug-11 6:56 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
Michael A. Cochran23-Aug-11 6:32
Michael A. Cochran23-Aug-11 6:32 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
stephen.darling23-Aug-11 7:00
stephen.darling23-Aug-11 7:00 
GeneralMandatory XKCD reference Pin
jsc4224-Aug-11 1:23
professionaljsc4224-Aug-11 1:23 
AnswerRe: How do I generate a number divisable by 5, and check it? Pin
Vite Falcon23-Aug-11 7:28
Vite Falcon23-Aug-11 7:28 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
Michael A. Cochran23-Aug-11 9:17
Michael A. Cochran23-Aug-11 9:17 
GeneralRe: How do I generate a number divisable by 5, and check it? Pin
fuximus23-Aug-11 15:40
fuximus23-Aug-11 15:40 
AnswerRe: How do I generate a number divisable by 5, and check it? Pin
KP Lee23-Aug-11 8:54
KP Lee23-Aug-11 8:54 
AnswerRe: How do I generate a number divisable by 5, and check it? Pin
vaibhav9223-Aug-11 19:34
vaibhav9223-Aug-11 19:34 
AnswerRe: How do I generate a number divisable by 5, and check it? Pin
lukeer23-Aug-11 22:40
lukeer23-Aug-11 22:40 
QuestionC# .net mass emailer Pin
markymark8222-Aug-11 3:38
markymark8222-Aug-11 3:38 
AnswerRe: C# .net mass emailer Pin
jschell22-Aug-11 9:24
jschell22-Aug-11 9:24 
GeneralRe: C# .net mass emailer Pin
markymark8222-Aug-11 22:15
markymark8222-Aug-11 22:15 
GeneralRe: C# .net mass emailer Pin
BobJanova22-Aug-11 23:51
BobJanova22-Aug-11 23:51 
GeneralRe: C# .net mass emailer Pin
markymark8223-Aug-11 0:01
markymark8223-Aug-11 0:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.