Last active
January 3, 2016 22:38
-
-
Save arivero/8529263 to your computer and use it in GitHub Desktop.
reparto aleatorio, con crecimiento de nodos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
from array import array | |
import sys | |
total=10000 | |
#reparto=20000 | |
reparto=int(sys.argv[1]) | |
cantidad=array('l',(1 for x in xrange(total))) | |
for i in xrange(reparto-total): | |
concede=randint(1,total) | |
x, ticks =-1,0 | |
while ticks < concede: | |
x+=1 | |
ticks+=cantidad[x] | |
cantidad[x]+=1 | |
total+=1 | |
#burbuja | |
while (x > 0) and (cantidad[x] > cantidad[x-1]): | |
cantidad[x],cantidad[x-1]= cantidad[x-1],cantidad[x] | |
x=x-1 | |
print cantidad[:20] | |
ricos=sum(cantidad[:len(cantidad)/100]) | |
print "hemos repartido ", total , " entre ", len(cantidad), | |
print " (media=",1.0*total/len(cantidad), ")" | |
print "el 1% posee el ", 100.0*ricos/total, "% del reparto" | |
print "el mas rico posee ", 1.0*cantidad[0]*len(cantidad)/total, | |
print "veces mas que la media" | |
######################### OTRO: | |
reparto=10 | |
while True: | |
for i in xrange(reparto): | |
concede=randint(1,total) | |
x, ticks =-1,0 | |
while ticks < concede: | |
x+=1 | |
ticks+=cantnum[x] # cantidad[x]*numero[x] | |
numero[x]-=1 | |
cantnum[x]-=cantidad[x] | |
if (x>0): | |
numero[x-1]+=1 | |
cantnum[x-1]+=cantidad[x-1] | |
else: | |
numero.insert(x,1) | |
cantnum.insert(x,cantidad[x]+1) #ojo, antes de modificar cantidad!! | |
cantidad.insert(x,cantidad[x]+1) | |
total+=1 | |
#ahora anadimos un nuevo nodo con cantidad 1 | |
x=len(cantidad)-1 | |
numero[x]+=1 | |
cantnum[x]+=1 | |
total+=1 | |
reparto=(reparto*2)%1000000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment