How do I generate a number divisable by 5, and check it?
-
Generate a four digit random number, multiply it by 10, add 5 (or 10). The result will be divisible by 5.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
This solution is not correct - it exhibits a possible overflow error into 6 digits. Consider: 4 Random digits all come out as 9 (multiply by 10 = 99990), then add 10 you get 100000 which does not meet the requirements... The correct solution is: Generate a four digit random number Multiply it by 10 (to shift into correct position) Then randomly choose to make the last digit a 5 or a 0 (adding 10 is the incorrect step) The result is divisible by 5. Regards David.
-
I KNEW there was a reason why I liked num -= num%5; better! :) How often have you seen a sizable random number generator produce 0 as an answer? (OK, you can easily set your random number limit so *10 will never overflow.) The original request is divisable by 5 and only affecting the last digit, not the whole number. Lets see: convert the int to a string, take a substring 1 less than the length, add "5" to the string and then convert back to int. There's quite a few ways to get this to work, multiplying by 10 and adding 5 will ONLY work correctly when the random number IS zero. Exactly where did I say to divide by zero? If num is zero, num=(num/5)*5 will produce zero. The highest numbers in int16, int, and int64 all end in decimal 7 and this process will work in all languages, the lowest number ends in 8. That will work in reasonable languages, VB.NET will blow up with an overflow. (In that language use num=(num\5)*5) (Actually VB is a reasonable language, you just have to know the gotchas like 5/3 is 2, not 1. I spent a little time swearing at its math until I found "\". I haven't found a use for round-up but if I did, VB would actually work quite a bit better than most languages for that.) Also good luck executing 5/0! (Or ANY other number by 0)
-
x = generate random number between 10000 and 99995 remainder to be added = 5 - x MOD 5 Therefore, the resultant random number should be x + 5 - x MOD 5 Here's a sample code: http://ideone.com/LxgrC[^]
a.k.a. Vite Phoenix and Vite Zeus... Proud member and co-founder of OlympianZ
-
Hi. First, how would I create a random number, and then add the last digit, so that it is divisable by 5? the number should always be 5 digits long. Second, how do I check it, I think I need to do something like...
if (int x MOD 5 ==0)
Or something like that. The first step is the most important though. Thank you, Steve
-
Hi. First, how would I create a random number, and then add the last digit, so that it is divisable by 5? the number should always be 5 digits long. Second, how do I check it, I think I need to do something like...
if (int x MOD 5 ==0)
Or something like that. The first step is the most important though. Thank you, Steve
The solution I found most elegant had already been posted by David1984 but is deleted. So I'm going to post it now: You want a number X that can be divided by 5. Any number Y can be multiplied by 5. The result X is divisible by definition (and by 5). You want X to have 5 digits. 5-digit-numbers range from 10000 to 99999. Divide both by 5 and throw away the result's decimal places to get the limits for Y (2000 to 19999). Supposed you already have an instance of
Random
obeying the tips on its use, you get X by using this code (X changed to a meaningful name)int randomDivisibleByFive = random.Next(2000, 19999) * 5;
Ciao, luker
-
// 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 ze -
Yes, I did read that, I was tweaking. Seriously, multiplying exposes an overflow possiblity. And why 10? num = (num/5)*5; does the trick num -= num%5; also assures overflow won't occur even if you are within 5 of overflow in either positive or negative direction.
Did you read the bit that says generate a four digit number? That won't generate an overflow. :) The OP wanted 5 digit numbers.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
-
This solution is not correct - it exhibits a possible overflow error into 6 digits. Consider: 4 Random digits all come out as 9 (multiply by 10 = 99990), then add 10 you get 100000 which does not meet the requirements... The correct solution is: Generate a four digit random number Multiply it by 10 (to shift into correct position) Then randomly choose to make the last digit a 5 or a 0 (adding 10 is the incorrect step) The result is divisible by 5. Regards David.
See my correction - you don't need to add 10 at all (you can add 5 without fear). That was a silly on my part. :)
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
-
Did you read the bit that says generate a four digit number? That won't generate an overflow. :) The OP wanted 5 digit numbers.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
riced wrote:
Did you read the bit that says generate a four digit number? That won't generate an overflow. :)
The OP wanted 5 digit numbers.1. No, I ignored the 5 digit requirement on first reading. My bad. 2. No, I didn't see "generate a four digit number" in the original post and on re-reading still don't see it. Must be written in invisible ink. 3. You are right, no overflow. (Really dumb to use int16 on a 5 digit number because of good chance for overflow.) I'm going to assume he intended to generate a 5 digit number because he's asking how to modify the last digit. It would be really dumb to ask how to modify the last digit to be divisable by 5 if you start out with 4 digits and want 5. A lot of random number generators produce a number between 0 and 1. You can't get a negative number from that and real numbers have at best 6 digits of accuracy so keeping it down to 5 places makes sense. If you multiply the real number by 100K it is possible to get a number below 10K. So, set your int field to 0, while it is 0 set it to the random number times 100K. (Hopefully not an infinite loop :) ) While it is less than 10K multiply by 5. Then subtract the remainder of 5.
-
riced wrote:
Did you read the bit that says generate a four digit number? That won't generate an overflow. :)
The OP wanted 5 digit numbers.1. No, I ignored the 5 digit requirement on first reading. My bad. 2. No, I didn't see "generate a four digit number" in the original post and on re-reading still don't see it. Must be written in invisible ink. 3. You are right, no overflow. (Really dumb to use int16 on a 5 digit number because of good chance for overflow.) I'm going to assume he intended to generate a 5 digit number because he's asking how to modify the last digit. It would be really dumb to ask how to modify the last digit to be divisable by 5 if you start out with 4 digits and want 5. A lot of random number generators produce a number between 0 and 1. You can't get a negative number from that and real numbers have at best 6 digits of accuracy so keeping it down to 5 places makes sense. If you multiply the real number by 100K it is possible to get a number below 10K. So, set your int field to 0, while it is 0 set it to the random number times 100K. (Hopefully not an infinite loop :) ) While it is less than 10K multiply by 5. Then subtract the remainder of 5.
Here's my original post: (the add 10 is redundant - me being silly)
Generate a four digit random number,
multiply it by 10,
add 5 (or 10).
The result will be divisible by 5.The adding 5 could be done at random. The Random class in C# allows you to get next integer in a range e.g. x = Random.Next(0, 10000) gets you a number on range 0 to 9999 (the second parameter is the exclusive upper limit). So no need to mess about multiplying by 100k. He could use Random.Next(1000, 10000) if he actually requires, or just use String formatting if it's just to be displayed.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
-
Kenneth Kasajian wrote:
Is this discussion really taking place? What is this, 3rd grade?
What is everones problem? I asked how to solve a basic math based problem PROGMATICALLY as I am a beginner when it comes to c#, and I am wishing I never bothered asking! Although, there are a number of people who have offered working solutions. Why must people post such a responce as yours? Did your post really help in anyway? Regards, Stephen
Let me be clear. I don't have any problem with people asking very basic programming questions. In fact, I encourage it. I don't expect people to know everything about programming. I was commenting on this discussions of what numbers are divisible by 5, as in: "However, I was under the impression I could generate numbers that would be divisible by 5, but not end in 5? Is this wrong?" You're kidding me right? You don't know that the numbers 10 and 20 are divisible by 5? Clearly they don't end in 5. My comment was no to the programming question.
-
Let me be clear. I don't have any problem with people asking very basic programming questions. In fact, I encourage it. I don't expect people to know everything about programming. I was commenting on this discussions of what numbers are divisible by 5, as in: "However, I was under the impression I could generate numbers that would be divisible by 5, but not end in 5? Is this wrong?" You're kidding me right? You don't know that the numbers 10 and 20 are divisible by 5? Clearly they don't end in 5. My comment was no to the programming question.
Kenneth Kasajian wrote:
You're kidding me right?
Are you? This thread was cleared up a while ago, and I am not going to explain myself yet again :) Regards!
-
Kenneth Kasajian wrote:
You're kidding me right?
Are you? This thread was cleared up a while ago, and I am not going to explain myself yet again :) Regards!
Interesting. So you're responding to say that you're not going to respond. Just don't bother dude. LOL
-
What Luc meant was that you should just create a random integer number and the go and multiply it by five. This will always give you a number which is divisible by 5, to state the obvious. The only thing I would add is if you are given a range in which the numbers should lie you'd need some adjustment.
Random rnd = new Random();
int lowerBound = 2001; // not divisable 5 five
lowerBound = lowerBound + ( 5 - lowerBound % 5 ); // make lowerBound divisable by fiveint upperBound = 10023; // not divisable by five
upperBound = upperBound - ( upperBound % 5 ); // make upperBound divisable by fiveint range = (upperBound - lowerBound) / 5; // calculate the range of numbers
int number = lowerBound + rnd.Next( range ) * 5; // presto
Cheers!
—MRB
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
surely the generated number would exceed the upper bounds: upper bound = 10023 modded down to 10020 random number generated between lower bound and modded upper bound = anything upto 10020 10020 * 5 = 50100 50100 > 10023 perhaps divide the bounds by 5 first
-
Is this a joke question? Generate a four digit number and add an extra number to the end, being either 5 or 0. Then don't check it because it will be correct!!! e.g. 4678; add 5 on the end to give 46785.
Exactly my take on it ;)