| 1. |
Solve : Multiple Check? |
|
Answer» Okay, say I have a variable, it is a number. Okay, say I have a variable, it is a 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.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. |
|