溫馨提示×

如何自定義rand函數(shù)的種子值

PHP
小樊
87
2024-08-07 10:54:19
欄目: 編程語言

要自定義rand函數(shù)的種子值,可以使用srand函數(shù)來設(shè)置種子值。下面是一個示例代碼:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    int seed;
    
    printf("Enter a seed value: ");
    scanf("%d", &seed);
    
    srand(seed);
    
    for (int i = 0; i < 5; i++) {
        printf("%d\n", rand());
    }
    
    return 0;
}

在這個示例代碼中,用戶可以輸入一個種子值,然后使用srand函數(shù)設(shè)置該種子值。接下來,通過調(diào)用rand函數(shù)生成隨機數(shù)。這樣就實現(xiàn)了自定義rand函數(shù)的種子值。

0