#!/usr/bin/env ruby require 'gtk2' require 'dbus' require 'digest/sha1' module RandomClient class Client def initialize(max) @max = max bus = DBus.session_bus random_service = bus.service("org.muadda.random") objname = sprintf("/org/muadda/random%d", @max) puts "Connecting to #{objname}" @random_obj = random_service.object(objname) @random_obj.introspect @random_obj.default_iface = "org.muadda.random" end def choose r1 = rand(0xFFFFFFFF) r2 = rand(0xFFFFFFFF) b1 = rand(@max) h = Digest::SHA1.hexdigest(sprintf("%d-%d-%d", r1, r2, b1)) b2, = @random_obj.suggest_random(h, r1) @random_obj.confirm_random(h, r2, b1) (b1 + b2).modulo(@max) end end srand DBus::SessionBus.instance.glibize max = 16 c = Client.new(max) hist = [] (0..max-1).each { |i| hist[i] = 0 } (1..100).each { tirage = c.choose puts "tirage = #{tirage}" hist[tirage] += 1 } (0..max-1).each { |i| puts "#{i} occured #{hist[i]} times" } end