|
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
|
|
|
|
|
a random multiple of five is bound to be five times some other random number.
|
|
|
|
|
Luc Pattyn wrote: a random multiple of five is bound to be five times some other random number.
?
What do you mean?
Regards,
Stephen
|
|
|
|
|
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;
lowerBound = lowerBound + ( 5 - lowerBound % 5 );
int upperBound = 10023;
upperBound = upperBound - ( upperBound % 5 );
int range = (upperBound - lowerBound) / 5;
int number = lowerBound + rnd.Next( range ) * 5;
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
|
|
|
|
|
Stating the obvious! 5+
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
|
|
|
|
|
I'd say you're stating the obvious, but then, I remember when I was very young and inexperienced and I couldn't see stuff like this. So, it's not always obvious as one might think.
You got my five.
"To alcohol! The cause of, and solution to, all of life's problems" - Homer Simpson
|
|
|
|
|
I tend to state facts, even obvious ones, especially when it seems to OP is missing them somehow. Rather than spoon feeding, I prefer to give a gentle push in the right direction...
|
|
|
|
|
Completely agree, I'm totally against the "gimme codezzzz plz" culture.
"To alcohol! The cause of, and solution to, all of life's problems" - Homer Simpson
|
|
|
|
|
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.
|
|
|
|
|
Does this mean that no matter what number I generate, it will always have to end in 5?
e.g. All these numbers are divisable by 5
39485
99045
12095
49385
99335
However, I was under the impression I could generate numbers that would be divisable by 5, but not end in 5?
Is this wrong?
Regards,
Stephen
|
|
|
|
|
Numbers that end in 0 are also divisible by 5 (except zero)
So you're right.
|
|
|
|
|
|
It depends on the definition of divisibility that you use.
Zero can also be divisible by anything, if you use an other definition.
|
|
|
|
|
BS.
zero times x equals zero, no matter what (finite) value x has.
so (the right side's) zero is divisible by x, and the result is (the left side's) zero.
If I hold 10 pies, 5 bacon sandwiches, and zero glasses of milk, I have no problem distributing them evenly to 5 people.
Next you'll state you could also redefine 5, so it no longer divides itself.
|
|
|
|
|
Luc Pattyn wrote: Next you'll state you could also redefine 5, so it no longer divides itself.
It doesn't. There are only four bacon sandwiches left...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
Sorry for the late reply, I have been off-line this evening, I have another tournament going on this week. I trust all bacon sandwiches have magically disappeared by now, and so the problem got solved?
|
|
|
|
|
Nope. You seem to think that that is the only definition of divisibility.
I did not personally redefine anything.
There is no natural number n such that 0/x=n so no x evenly divides 0.
If you use the definition with integers instead of natural numbers, everything divides zero.
Also, the prime factorization of zero is empty.
|
|
|
|
|
natural numbers are the ordinary counting numbers 1, 2, 3, ... (sometimes zero is also included) is what Wikipedia[^] offers as a definition. Now you can choose: either you include zero and you are allowed to use it at both sides of your 0/x=n , or you exclude it (and then your "except zero" remark that started all this is completely irrelevant).
|
|
|
|
|
Yes but that's precisely the point, you can choose.
|
|
|
|
|
That is a stupid definition. It would also indicate that -10 is not divisible by 5. There may be abstruse mathematical concepts for which it's useful, I suppose, but for normal maths it is nonsense.
|
|
|
|
|
x is not a natural number there.
|
|
|
|
|
And your point is? (Neither is 0, right?) Both are representable in the number system, and both divide by 5 in any reasonable definition.
|
|
|
|
|
http://mathworld.wolfram.com/EvenNumber.html[^]
Note zero is an even number and by definition dividable by two, the answer is just zero. As a recall from calculus I, you can't divide a number by zero but you can divide a number as the devisor approaches zero.
ARon
|
|
|
|
|
Actually 0 is divisable by any number.
|
|
|
|