Random Math

16 processes running at once, all scrolling fast enough to refresh every screen every frame.

I was telling the kids how fast computers are, and to demonstrate I wrote this Python program to do simple multiplication, division, addition, and subtraction.

from random import random as rand
from random import choice
def randmath():
	ops = {'+': 31, '-': 32, '*': 16, '/': 32}
	operator = choice([i for i in ops.keys()])
	scope = 1 << ops[operator]
	num1 = int(rand()*scope)
	num2 = int(rand()*scope)
	mathstring = "{} {} {}".format(num1, operator, num2)
	result = eval( mathstring )
	print(mathstring , "=", result)
	
while True: randmath()

Since that scrolls by so quickly, you may want to try this version, which pauses two seconds between calculations.

from random import random as rand
from random import choice
from time import sleep
def randmath():
	ops = {'+': 31, '-': 32, '*': 16, '/': 32}
	operator = choice([i for i in ops.keys()])
	scope = 1 << ops[operator]
	num1 = int(rand()*scope)
	num2 = int(rand()*scope)
	mathstring = "{} {} {}".format(num1, operator, num2)
	result = eval( mathstring )
	print(mathstring , "=", result)
	
while True:
        randmath()
        sleep(2)
Twenty throttled math programs all running at once, with the CPU hovering around 3-10%

To get them all nicely tiled like that, right-click on the taskbar and select “Show windows stacked”

About Ziggy

I strive to be awesome for God. Support my efforts at: http://sub.tryop.com
This entry was posted in Articles and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published.