
- Python 基礎
- Python - 首頁
- Python - 概述
- Python - 歷史
- Python - 特性
- Python vs C++
- Python - Hello World 程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境搭建
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode 系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python 控制語句
- Python - 控制流
- Python - 決策
- Python - if 語句
- Python - if else
- Python - 巢狀 if
- Python - match-case 語句
- Python - 迴圈
- Python - for 迴圈
- Python - for-else 迴圈
- Python - while 迴圈
- Python - break 語句
- Python - continue 語句
- Python - pass 語句
- Python - 巢狀迴圈
- Python 函式與模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 僅關鍵字引數
- Python - 位置引數
- Python - 僅位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python 字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python 列表
- Python - 列表
- Python - 訪問列表元素
- Python - 修改列表元素
- Python - 新增列表元素
- Python - 刪除列表元素
- Python - 列表迴圈
- Python - 列表推導式
- Python - 排序列表
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python 元組
- Python - 元組
- Python - 訪問元組元素
- Python - 更新元組
- Python - 解包元組
- Python - 元組迴圈
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python 集合
- Python - 集合
- Python - 訪問集合元素
- Python - 新增集合元素
- Python - 刪除集合元素
- Python - 集合迴圈
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python 字典
- Python - 字典
- Python - 訪問字典元素
- Python - 修改字典元素
- Python - 新增字典元素
- Python - 刪除字典元素
- Python - 字典檢視物件
- Python - 字典迴圈
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python 陣列
- Python - 陣列
- Python - 訪問陣列元素
- Python - 新增陣列元素
- Python - 刪除陣列元素
- Python - 陣列迴圈
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python 檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS 檔案/目錄方法
- Python - OS 路徑方法
- 面向物件程式設計
- Python - OOPs 概念
- Python - 類與物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝類
- Python - 列舉
- Python - 反射
- Python 錯誤與異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except 塊
- Python - try-finally 塊
- Python - 丟擲異常
- Python - 異常鏈
- Python - 巢狀 try 塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python 多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 結束執行緒
- Python - 執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python 同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python 網路程式設計
- Python - 網路程式設計
- Python - Socket 程式設計
- Python - URL 處理
- Python - 泛型
- Python 庫
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 雜項
- Python - 日期與時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI 程式設計
- Python - XML 處理
- Python - GUI 程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送郵件
- Python - 擴充套件
- Python - 工具/實用程式
- Python - GUIs
- Python 高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - Monkey Patching
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python 資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 資源
- Python - 討論
- Python 編譯器
- NumPy 編譯器
- Matplotlib 編譯器
- SciPy 編譯器
Python random.setstate() 函式
Python 中的 **random.setstate()** 函式用於將生成器的內部狀態恢復到呼叫 **getstate()** 時所處的狀態。狀態引數應從之前對 **getstate()** 的呼叫中獲得。此函式是 random 模組的一部分,該模組提供各種函式來生成隨機數和序列。
此函式的主要目的是將隨機數生成器的內部狀態恢復到先前捕獲的狀態。
**注意** − 此函式不能直接訪問,因此我們需要匯入 random 模組,然後需要使用 random 靜態物件呼叫此函式。
語法
以下是 random.setstate() 函式的語法:
random.setstate(state)
引數
Python random.setstate() 函式接受單個引數:
**state** − 一個物件,捕獲生成器的當前內部狀態,該狀態是從之前對 getstate() 的呼叫中獲得的。
返回值
random.setstate() 函式不返回值。
示例 1
讓我們來看一個使用 Python **random.setstate()** 函式的示例。
在下面的程式碼中,定義了一個長度為 15 的列表,然後 **random.setstate()** 捕獲隨機數生成器的當前狀態。然後,程式碼使用當前隨機狀態生成一個大小為 10 的列表。此後,使用 **random.setstate()** 恢復狀態,這確保後續大小為 5 的輸出列表與之前具有相同的隨機性。
import random list=[11,12,13,14,15,16,17,18,19,20,21,22,23,24,25] state = random.getstate() print(random.sample(list, k = 10)) random.setstate(state) print(random.sample(list, k = 5))
以下是程式碼的輸出:
[23, 21, 18, 11, 25, 16, 19, 22, 24, 13] [23, 21, 18, 11, 25]
示例 2
在這個例子中,我們將使用random.setstate()函式來恢復隨機數生成器的狀態到之前捕獲的狀態。
import random # Initialize the random number generator random.seed(42) # Generate a sample of 10 numbers from a range of 20 print(random.sample(range(30), k=10)) # Capture the current state state = random.getstate() # Generate a sample of 20 numbers from a range of 20 print(random.sample(range(20), k=20)) # Restore the state using the setstate() function random.setstate(state) # Generate another sample of 10 numbers from the same range print(random.sample(range(20), k=10))
執行上述程式後,會產生以下結果:
[20, 3, 0, 23, 8, 7, 24, 4, 28, 17] [2, 18, 13, 1, 0, 16, 3, 17, 8, 9, 15, 11, 12, 5, 6, 4, 7, 10, 14, 19] [2, 18, 13, 1, 0, 16, 3, 17, 8, 9]
示例 3
在這個例子中,我們將演示如何使用random.setstate()函式透過捕獲和恢復隨機數生成器的狀態來重現相同的隨機數。
import random # Initialize the random number generator and get state random.seed(0) initial_state = random.getstate() # Generate and print random number print(random.random()) print(random.random()) # Setting the seed back to 0 resets the RNG back to the original state random.seed(0) new_state = random.getstate() assert new_state == initial_state # Since the state of the generator is the same as before, it will produce the same sequence print(random.random()) # We could also achieve the same outcome by resetting the state explicitly random.setstate(initial_state) print(random.random())
上述程式碼的輸出如下:
0.8444218515250481 0.7579544029403025 0.8444218515250481 0.8444218515250481
示例 4
這是一個比較使用random.seed()、random.setstate()和random.setstate()函式生成隨機數所需時間的另一個示例。
import random import timeit # Measure the time taken to generate random numbers using seed() t1 = timeit.timeit(stmt="""random.seed(42) random.randint(1, 10)""", number=10000, setup="import random") # Measure the time taken to generate random numbers using setstate() and setstate() t2 = timeit.timeit(stmt="""random.randint(1, 10) random.setstate(state)""", number=10000, setup="""import random state = random.getstate()""") print("Time taken using seed():", t1) print("Time taken using setstate() and setstate():", t2)
以下是上述程式碼的輸出:
Time taken using seed(): 0.12103769998066127 Time taken using setstate() and setstate(): 0.06645569996908307