Menghasilkan nomor urut unik dengan python

Artikel ini adalah tentang modul import random random.seed(1) # Get the state of the generator state = random.getstate() print('Generating a random sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) # Restore the state to a point before the sequence was generated random.setstate(state) print('Generating the same identical sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) 5 dengan Python, yang digunakan untuk menghasilkan angka acak semu untuk berbagai distribusi probabilistik

Metode Modul acak Python

1. benih()

Ini menginisialisasi generator nomor acak. Untuk menghasilkan urutan acak baru, benih harus diatur tergantung pada waktu sistem saat ini. import random random.seed(1) # Get the state of the generator state = random.getstate() print('Generating a random sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) # Restore the state to a point before the sequence was generated random.setstate(state) print('Generating the same identical sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) 6 menetapkan benih untuk pembuatan angka acak

2. getstate()

Ini mengembalikan objek yang berisi keadaan generator saat ini. Untuk memulihkan status, teruskan objek ke import random random.seed(1) # Get the state of the generator state = random.getstate() print('Generating a random sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) # Restore the state to a point before the sequence was generated random.setstate(state) print('Generating the same identical sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) 7

3. setstate(status_obj)

Ini mengembalikan status generator pada saat import random random.seed(1) # Get the state of the generator state = random.getstate() print('Generating a random sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) # Restore the state to a point before the sequence was generated random.setstate(state) print('Generating the same identical sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) 8 dipanggil, dengan meneruskan objek status

4. getrandbits(k)

Ini mengembalikan bilangan bulat Python dengan import random random.seed(1) # Get the state of the generator state = random.getstate() print('Generating a random sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) # Restore the state to a point before the sequence was generated random.setstate(state) print('Generating the same identical sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) _9 bit acak. Ini berguna untuk metode seperti Generating a random sequence of 3 integers... 138 583 868 Generating the same identical sequence of 3 integers... 138 583 868 _0 untuk menangani rentang besar yang berubah-ubah untuk pembuatan angka acak

>>> import random >>> random.getrandbits(100) # Get a random integer having 100 bits 802952130840845478288641107953 _

Berikut adalah contoh untuk mengilustrasikan metode import random random.seed(1) # Get the state of the generator state = random.getstate() print('Generating a random sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) # Restore the state to a point before the sequence was generated random.setstate(state) print('Generating the same identical sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) _8 dan import random random.seed(1) # Get the state of the generator state = random.getstate() print('Generating a random sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) # Restore the state to a point before the sequence was generated random.setstate(state) print('Generating the same identical sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) 7

import random random.seed(1) # Get the state of the generator state = random.getstate() print('Generating a random sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000)) # Restore the state to a point before the sequence was generated random.setstate(state) print('Generating the same identical sequence of 3 integers...') for i in range(3): print(random.randint(1, 1000))

Kemungkinan Keluaran

Generating a random sequence of 3 integers... 138 583 868 Generating the same identical sequence of 3 integers... 138 583 868

Hasilkan Bilangan Bulat Acak

Modul acak menyediakan beberapa metode khusus untuk menghasilkan bilangan bulat acak

1. randrange (mulai, berhenti, langkah)

Mengembalikan bilangan bulat yang dipilih secara acak dari Generating a random sequence of 3 integers... 138 583 868 Generating the same identical sequence of 3 integers... 138 583 868 3. Ini menimbulkan Generating a random sequence of 3 integers... 138 583 868 Generating the same identical sequence of 3 integers... 138 583 868 4 jika Generating a random sequence of 3 integers... 138 583 868 Generating the same identical sequence of 3 integers... 138 583 868 5 > Generating a random sequence of 3 integers... 138 583 868 Generating the same identical sequence of 3 integers... 138 583 868 6

2. randint(a, b)

Mengembalikan bilangan bulat acak antara a dan b (keduanya inklusif). Ini juga menimbulkan Generating a random sequence of 3 integers... 138 583 868 Generating the same identical sequence of 3 integers... 138 583 868 4 jika Generating a random sequence of 3 integers... 138 583 868 Generating the same identical sequence of 3 integers... 138 583 868 8 > Generating a random sequence of 3 integers... 138 583 868 Generating the same identical sequence of 3 integers... 138 583 868 9

Berikut adalah contoh yang menggambarkan kedua fungsi di atas

import random i = 100 j = 20e7 # Generates a random number between i and j a = random.randrange(i, j) try: b = random.randrange(j, i) except ValueError: print('ValueError on randrange() since start > stop') c = random.randint(100, 200) try: d = random.randint(200, 100) except ValueError: print('ValueError on randint() since 200 > 100') print('i =', i, ' and j =', j) print('randrange() generated number:', a) print('randint() generated number:', c)

Kemungkinan Keluaran

ValueError on randrange() since start > stop ValueError on randint() since 200 > 100 i = 100 and j = 200000000.0 randrange() generated number: 143577043 randint() generated number: 170

Menghasilkan angka floating point acak

Mirip dengan menghasilkan bilangan bulat, ada fungsi yang menghasilkan urutan floating point acak

  • acak. random() -> Mengembalikan angka floating point acak berikutnya antara [0. 0 sampai 1. 0)
  • acak. uniform(a, b) -> Mengembalikan floating point acak import random i = 100 j = 20e7 # Generates a random number between i and j a = random.randrange(i, j) try: b = random.randrange(j, i) except ValueError: print('ValueError on randrange() since start > stop') c = random.randint(100, 200) try: d = random.randint(200, 100) except ValueError: print('ValueError on randint() since 200 > 100') print('i =', i, ' and j =', j) print('randrange() generated number:', a) print('randint() generated number:', c) 0 sehingga a

Postingan terbaru

LIHAT SEMUA