Sunday, January 29, 2012

Randomness in Batch & Reality

So this weekend I've been working on the code for my first project, which has a lot to do with Randomness. This got me to thinking about how we define 'random' both in coding and in reality. Without trying to bore you guys too much I have written some info regarding the dynamic random variable in batch script.

I began by including this phrase as part of my batch file, with 'n' being the random file that will end up being selected:
set /a "n = %random% %% %n% + 1"
This equation is meant to randomly generate a number between 1 and the number of media files on any given computer. The problem I found with this is that having only one random variable allowed 'n' to become dependent on the modulus operation that followed, which somehow was connected with system time. This means that after the program picks a random file, chances are pretty likely that when I run the script again right afterwards it will pick a file that is very near the first, such as a picture in a folder being picked, and then another picture in that same folder. To address this problem I modified it to become this:
set /a "n = (%random% * 32768 + %random%) %% %n% + 1"
 This equation multiplies a random number with 32768 (which is the limit to random numbers under a single variable in batch script, I assume to decrease file size) then adding another random variable before performing the modulo operation (written as %% in a batch file) This helps slightly because the modulo operation always returns the remainder of a division, which means that if the first operator (random number) is less than the second, the remainder will always be same as the first operator. For example 3 %% 5 = 3 because 3/5 = 0 with a remainder of 3. This why the first equation was lacking in 'randomness' and why multiplying two variables gives me a larger range of random numbers to choose from. However, following in the vain that creating a larger range of random numbers to choose from gives me a greater chance of a completely different file (which are numbered by another part of my script) on any given computer, I ended up using this equation:
set /a "n = (((%random% & 1) * 1073741824) + (%random% * 32768) + %random%) %% %n% + 1"
This uses only 1 bit from the first number, so if there is some problem with the numbers being close, it will largely be ignored (The only information used from the first number is whether it is odd or even.) Multipying two random variables together (32768 * 32768) gives me 1073741824. The method also yields a range of 2147483647 possibilities, which is the limit to 'randomess' in Batch coding. Trying to go further will just make the numbers uselessly negative or something, unless you use more than one variable to hold it, and make your own methods to go beyond what the system supports.

Now that I have sufficiently bored everyone to death with the ennui of Batch file coding, this all boils down to the fact that picking random files off any given computer isn't picking a random file after another, its actually set depending on the order the choices happen to be in. The random files have already been set, it's just the process of progressing through the line of 'random' files. The higher the fraction (random number range/things to choose from) the less significant this is. The 'purest randomness' happens when that fraction equates to a positive whole number. If the fraction is less than 1, some files will never be chosen.

If we can be sure that there never is actually a pure randomness in regards to computing, can there exist a pure randomness in reality either? And because we tend to view a random outcome as completely without bias, such as Caillois' alea, can this be exploited in a game setting? Also, as we saw that there is always a possibility that a file will never be chosen, how does that relate to reality; just because something never occurred in history, does that mean that it doesn't still exist?

No comments:

Post a Comment