1.

Solve : Multiple Check?

Answer»

Okay, say I have a variable, it is a number.
How WOULD I check to see if it is divisible by 4, or by any other number.
Also, how would I create variables that are multiples of 4, or any other number. For instance %random%*4 may not be fully divisible by 4 and may be 4.1232424, which is not correct.

This is for HELP WITHIN a key generator, Say a customer has the key 8-16-4-12-24. They are all divisible by 4, so it returns true, just as an example. How would I produce a series of 5 numbers that are multiples of 4 and then check to see if they divisible by 4.
Thanks for your help.Wow, I think I have done it.
Take a look at this CODE, this file gets a random number, keeps taking away 4, if it ever gets to the number 4, it means that the number can be divided by 4 and therefore echos it. If it does not get to exactly 4 then it starts again with a new random number.
Code: [Select]@echo off
echo Creating Key...
:start
set key=%random%
set start=%key%
:divide
set /a key=%key%-4
If %key% LSS 1 (goto start)
If %key% NEQ 4 (goto divide)
If %key% EQU 4 (
echo %start% is divisible by 4.
pause >nul
)

yea it works Quote from: Jacob on December 14, 2008, 07:53:43 AM

Okay, say I have a variable, it is a number.
How would I check to see if it is divisible by 4, or by any other number.

Another way is to use the modulus operator which is two percent signs in a batch or one at the prompt

N modulus M means "the remainder left over after N is divided by M" and is zero if N can be divided exactly by M.

It is usually abbreviated to N mod M.

Thus:

17 mod 4 = 1

and

16 mod 4 = 0

(Any number) mod (4) will equal 0, 1, 2, or 3 at most, obviously.

Code: [Select]@echo off

:loop

set /a number=%random%

echo random number=%number%

set /a remainder=%number% %% 4

if %remainder% NEQ 0 goto loop

echo %number% is divisible by 4


Quote
For instance %random%*4 may not be fully divisible by 4 and may be 4.1232424, which is not correct.

In batch arithmetic, there are only integers (whole numbers) so that will never happen.

The number returned by %random% is an integer between 0 and 32767. So you may need to guard against %random% being zero, (which the code above will do.)

Quote
How would I produce a series of 5 numbers that are multiples of 4 and then check to see if they divisible by 4.

If they are multiples of 4 then they are by definition divisible by 4Thank you, very useful, I will be sure to use this. It is of course, also a lot faster than my method.You can use the modulus operator with %random% to restrict the range of random numbers generated.

Since %random%

...returns a number between 0 and 32767,

%random% %%N

...returns a number in the range 0 to N-1

so that:

set /a number=(%random% %% 20) + 1

...will provide a random number in the range 1 to 20





Quote from: Dias de verano on December 14, 2008, 01:58:32 PM
You can use the modulus operator with %random% to restrict the range of random numbers generated.

Since %random%

...returns a number between 0 and 32767,

%random% %%N

...returns a number in the range 0 to N-1

so that:

set /a number=(%random% %% 20) + 1

...will provide a random number in the range 1 to 20



Yes, I have used this method in my ACE combat engine to randomize damage between the players attack level , thanks for your help once again.


Discussion

No Comment Found