开发者

A function that generates a boolean that differs between computers, but not within the same computer. Possible?

This is kind of a weird question. I'd like to write a Java function that will either return true or false. It will ALWAYS return either true or false for the same computer, even if the program has been开发者_JAVA技巧 purged from the computer and reinstalled (that is, no state is allowed.) It will return the same value no matter where in the program it is called, what time it is, if it's a Tuesday, etc.

My second requirement is that for all of the computers in the world it there should be anywhere from a 50/50 to a 30/70 split in what the function returns. That is, at least 30% of computers need to generate the less-likely result of the function.

My third requirement (the tricky one) is that what causes the true/false split won't be obvious to users. So splitting along operating system lines isn't okay, since that's obvious.

Any thoughts?

Update: True, "same computer" doesn't have much meaning since computers are made of changeable parts. It's fine for the value to change if a piece of hardware is replaced.


Well, the most simple solution would be to just use the last digit of the mac address to return true or false based on if its even or odd. This will ensure that on the same machine it always returns the same for a machine and it will return true on half the machines in the world.

import java.net.*;


/**
 *
 * @author nick
 */
public class HardwareTruthGen {

   //Instance Variables 
    static InetAddress addr;
    static NetworkInterface net;
    static byte[] macAddr;
    static boolean hardware;

   static {

       try{  
        addr = InetAddress.getLocalHost();
        net = NetworkInterface.getByInetAddress(addr);
        macAddr = net.getHardwareAddress();

        //If mac address ends in an even number return true otherwise return false 
        if((macAddr[(macAddr.length - 1)] % 2) == 0)
            hardware = true;
        else
            hardware = false;
      }
      catch (Exception ex){

      }
    }

   public static boolean macTrue(){ return hardware;}

 }


Your specification lacks specificity, particularly in the definition of ALWAYS and same computer. Everything else hinges on your assumptions of those invariants.

Lets assume ALWAYS means "now" and same computer means "current JVM process", wildly wrong but illustrates the assumptions determine the outcome of your 2nd and 3rd requirements.

Collect data on some hardware, possibly all hardware and generate a hash based on that hardware data. The hardware detected defines same computer and by implication requires ALWAYS to be "now".

8aca8asad8ae8f8fs8sv8sdfsf8sfs8a8faa

There, that's mine :)

Now you can map the minimum and maximum possible values for this hash, and distribute your boolean valueOf accordingly.

The 3rd requirement is security by obscurity and thus, by definition, "you're doing it wrong" ;)


Simple. Have a function f(a, b) where a is whether this function has been executed on this computer before, and b gives the last result on this computer (or false if a is false). Then, for correct input, you can compute f as a ? b : (boolean)random(0 or 1).


Ok, let's put on our hypothectical Computer Science hat. To really be able to reproduce the desired behavior on the machine with no state, there has to be a repeatable function inherently present. My first thought was to examine some register, and switch off of the value of the zero bit, without changing it of course. That may not be random enough because it depends on what instructions the CPU has been executing prior to your code peeking under the covers.

The second thought was to use some inheirent value of the machine, like hostname or CPU-ID, but they can either change (hostname) or may not exist on all platforms.

That's when I noticed the 'Java' tag and realized that you might have an uphill road in front of you if you don't want to write cross-platform JRI (don't blame you) or rely on an intrinsic value of the JRE which could change when the next version comes out or they switch vendors.

You may be able to use clock-speed, even if you have to do your own timing loop and and use compliation switches to prevent the JIT compiler/JRE from optimizing the routine. That could give you a range of values to switch on. No direct speeds; find the number of milliseconds a specific instruction takes, and switch on whether the value is odd/even.

There's more things to try but they just get more wild the longer I think on the problem. Please update this post with an answer of what you end up using. I'd like to know how it turns out.


You can use systems hostname or ip address and encrypt it or a seed for a randomizer.

int seed = InetAddress.getLocalHost().hashCode();
// or
int seed = InetAddress.getLocalHost().getHostName().hashCode();

boolean evenRandom = new Random(seed).nextBoolean();
boolean rand_30_70 = new Random(seed).nextInt(100) > 30;

It should be noted that the SecurityManager can change this result or prevent the hostname being obtained. It can also prevent you obtaining a MAC address and any other low-level system information you might want to get.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜