# random number generator with leading digit of 1 30.1%, 2 15%, etc of the time from random import random, randint, shuffle from math import floor def benford_random(rates): random_percent = random() first_digit_list = [] for i in range(1,len(rates)+1): rate = rates[i-1] first_digit_list += ([i]*int(floor((rate*100)))) return first_digit_list def random_number(): random_numbers = [] first_digit_rates = [0.301, 0.176, 0.125, 0.097, 0.079, 0.067, 0.058, 0.051, .046] first_digits = benford_random(first_digit_rates) for first_digit in first_digits: num_digits = randint(2,4) current_number = first_digit*(10**(num_digits-1)) current_digit = num_digits-2 while current_digit >= 0: current_number += (randint(0,9)*(10**current_digit)) current_digit -= 1 random_numbers.append(current_number) shuffle(random_numbers) return random_numbers print random_number()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question