# File: random_veletlenszamok_generalasa.py import random # "%15s" a szoveget jobbra igazitja egy 15 karakteres helyen, elotte space-t tesz. for i in range(5): # lebegopontos veletlenszam: 0.0 <= szam < 1.0 print "%15s" % random.random(), # lebegopontos veletlenszam: 10 <= szam < 20 print "%15s" % random.uniform(10, 20), # egesz veletlenszam: 100 <= szam <= 1000 print "%6s" % random.randint(100, 1000), # egesz veletlenszam, csak parosok 100 <= szam < 1000 print "%6s" % random.randrange(100, 1000, 2)
# File: random_veletlenszamok_generalasa.out 0.758370001978 17.490234568 470 876 0.675916695155 13.5547552892 621 544 0.257316693618 16.8669978611 298 512 0.549614293021 10.2374212383 947 924 0.109914018939 15.5529005584 146 252
# File: random_lista_elemenek_veletlen_kivalasztasa.py import random # veletlen kivalasztas a listabol for i in range(5): print random.choice([1, "alma", 3, "repa", "szolo"])
# File: random_lista_elemenek_veletlen_kivalasztasa.out 3szolo 1 szolo alma
A Python Library random modul leÃrásában több fajta véletlenszám előállÃtási mód is található.
# File: random_gaussi_veletlenszamok_eloallitasa.py import random histogram = [0] * 20 print histogram # gyakorisag eloszlasi grafikon keszitese # gaussi zajjal. # atlag = 5, standard deviation = 1 normalis szoras, atlagos elteres for i in range(1000): i = int(random.gauss(5, 1) * 2) histogram[i] = histogram[i] + 1 print histogram # a hisztogram kinyomtatasa m = max(histogram) for v in histogram: print "*" * (v * 50 / m)
# File: random_gaussi_veletlenszamok_eloallitasa.out [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 0, 1, 5, 15, 46, 88, 157, 196, 200, 140, 88, 36, 20, 8, 0, 0, 0 , 0] * *** *********** ********************** *************************************** ************************************************* ************************************************** *********************************** ********************** ********* ***** **
You must be logged in to post a comment.