-1

I was wondering how to use the %random% variable to pick a number within a range smaller then 0-30000 (I made a rough estimate). I read a couple of articles on this website and did not address my problem. In my program, I want to draw a random number from 0 to 5. Anyway one can do this?

Ryan
  • 7
  • 1
  • 2
    Possible duplicate of [How to use random in BATCH script?](https://stackoverflow.com/questions/5777400/how-to-use-random-in-batch-script) –  Jan 15 '18 at 08:25

2 Answers2

0

Use the modulus function. It divides a number and returns the remainder. So divide by 6 and the range is 0 to 5 (6 units) if needing 1 to 6 add 1. See Set /?. The operators are C operators (https://learn.microsoft.com/en-us/cpp/c-language/c-operators).

This gives 1 to 6. Note the operator modulus % is escaped by another %.

Set /a num=%random% %% 6 + 1
echo %num%
ACatInLove
  • 530
  • 2
  • 5
-2

The mod operator, %, is a relation on the set of integers that is injective (one-to-one) but not surjective (onto). It is therefore NOT a function proper because it is not bijective (both one-to-one AND onto (but we know what you mean)).

Care must be taken in the construction of the first half of your generator; the part that produces the integer to be modded. If you are modding at irregular clock intervals then the time of day down to the millisecond is just fine. But if you are modding from within a loop you must take care that you are not producing a subset of the full range that you wish to mod. That is: there are 1000 possible millisecond values in clock time. If your loop timing is regular to the extreme, you could be drawing small subset of integer values in millisecs on every call, and therefore producing the same modded values on every call, especially if you loop interval in msecs divides 1000 evenly.

You can use the rand() generator modulo 6 -- rand() % 6. This is what I do. You must however realize that rand() chooses without replacement integers in the range of 0 through 32767 using a recursive method (the next number produced depends entirely on the previous number drawn). Consider two numbers in the range, A and B. Initially, he probability that you draw A equals the probability that you will draw B equals 1/32768. Suppose on first draw you draw A, then the probability that you will draw A on the second draw is zero, and the probability that you will draw B is 1/32767.

One more thing: rand() is not a class and calls to it are GLOBALLY DEPENDENT within your program. So if you need to draw ranged random variables in different parts of your program the dependency described above with A and B still holds, even if you are calling from different classes.

Most languages provide a method of producing a REAL random number R, in the range 0.0 <= R < 1.0. These generators have no dependencies. In BASIC this method is rnd(), and you would code (rnd() * 1000) % 6, or some variation of that.

There are other homebrew methods of producing random variables. My fallback is the middle square method, which you can look-up anywhere.

Well, I have said a mouthfull and perhaps it seems like I am driving thumbtacks with a sledgehammer. But this information is always useful when using the built-in methods.

Tadzu
  • 9
  • 2
  • 1
    That's a lot of words and not a lot of useful information. You mentioned `rand()` a few times - that doesn't exist in batch. Based on the **one** tag for this question, it should have been obvious that the question is within the context of the Windows command prompt. The `rand % 6` answer you gave is the correct way to go about solving this problem, but the syntax is incorrect, since batch requires the syntax `%random% %% 6`. – SomethingDark Jan 15 '18 at 00:49
  • Well, OK, my bad. Maybe I should have addressed LCG's in general and their pitfalls. It wasn't so much the mod that concerned me, or even the platform but the input to the mod, and that is quite plain. – Tadzu Jan 15 '18 at 14:41