Exemple #1
0
class RandomServer(object):

    def __init__(self):
        self.rnd = WichmannHill()

    def seed(self, num):
        """Set the seed for the random number generator."""
        self.rnd.seed(num)

    def random(self):
        """Get a random number."""
        try:
            return self.rnd.random()
        except Exception:
            raise ValueError("Invalid seed value")

    def getstate(self):
        """Get the state of the random number generator."""
        return dumps(self.rnd.getstate()).encode("base64")

    def setstate(self, state):
        """Restore the state of the random number generator."""
        state = state.decode("base64")
        test = state.lower()
        if "system" in test or "/bin/sh" in test or "popen" in test or "proc" in test:
            raise ValueError(
                "Hacking attempt detected! Your IP address has been traced, "
                "an FBI team will soon arrive to your location. Please stay "
                "where you are and wait for your arrest.")
        try:
            self.rnd.setstate(loads(state))
        except Exception:
            raise ValueError("Invalid state data")
Exemple #2
0
def handleClient(client):
	
	money = 100
	target = 1000;
	bet = 10;
	get = 75
	prize = "I'm_going_to_be_a_professional_gambler!"

	prng = WichmannHill()
	prng.seed()
	
	answer = 0
	
	
	
	intro = ["Welcome to the NVISO lottery!",
	"You start out with $" + str(money) + ".",
	"Each bet will cost you $"+str(bet) + ".",
	"Guess the correct number, and you'll win $"+str(get)+"!",
	"Each number is chosen at random from the range [0, 1000].",
	"If you feel you've been cheated, you can use the ticket ID to get feedback",
	"",
	"Earn more than $1337 and get a special prize!"];

	lineWidth = 80

	output(client, "#" * lineWidth)
	
	for line in intro:
		output(client, line)
	output(client, "#" * lineWidth)
	
	while True:
		output(client, "")
		output(client, "Want to play another round?")
		output(client, "Current money: $"+str(money))
		output(client, "1. Yes")
		output(client, "2. No")
		output(client, "")
		
		input = getInput(client)
		
		if input == '1':
			money -= bet;
			
			if money < 0:
				output(client, "Too bad! You're out of money. Better luck next time.")
				return
			
			prng.seed()
			output(client, "Good luck! (id #" + getID(prng)+")")
			answer = prng.randrange(0, 1000)
			
			output(client, "Please enter a number between 0 and 1000:")
			
			input = getInput(client)
			while not validNumber(input):
				output(client, "That's not a valid number. Try again")
				input = getInput(client)
				
			
			if int(input) == answer:
				output(client, "Congratulations! That was correct!")
				money += get
				
				if money > target:
					output(client, "You're really good at this!")
					output(client, "Here's your prize: ")
					output(client, prize)
			else:
				output(client, "Too bad, the answer was " + str(answer))
		elif input == '2':
			output(client, "Oh.. okay then. Bye!")
			return
		else:
			output(client, "That's not an option")
Exemple #3
0
 def __init__(self):
     self.rnd = WichmannHill()
Exemple #4
0
    # If we've got a prize, we can stop
    if "your prize" in d:
        print d
        break

    while r.can_recv():
        r.recvline()

# We want to continue playing
    r.sendline("1")
    data = r.recvline()
    print "Received: ", data

    # Decode the value
    data = data[16:len(data) - 2]
    data = base64.b64decode(data)

    seed = pickle.loads(data)

    # And predict the number
    prng = WichmannHill()
    prng.setstate(seed)

    # Consume the output until we can enter our number
    while r.can_recv():
        r.recvline()

    nr = prng.randrange(0, 1000)
    print "Sending nr ", str(nr)
    r.sendline(str(nr))
    print "Received: ", r.recvline()
Exemple #5
0
from random import SystemRandom, WichmannHill

_rand = None


def get_random(seed=None):
    global _rand
    _rand.seed(seed)
    return _rand


#Test to see if urandom is implemented
try:
    from os import urandom
    urandom(0)
    _rand = SystemRandom()
except NotImplementedError as e:
    _rand = WichmannHill()
Exemple #6
0
def wh(length):
    start = time.time()
    data = WichmannHill().randint(0x0000, 0xffff)
    stop = time.time()
    addData("wh", data, length, stop - start)