# 亂數 - 隨機亂數的應用產業 - 博奕 遊戲 統計 AI等領域必要的技術 - C標準指令有提供 - `<stdlib.h>`標頭檔 內部定義名詞:RAND_MAX亂數最大值32767 - lib=library - 指令 - `srand()` - 種子亂數 - `rand()` - 用法是不用加參數 - 他會從內建亂數表取一個值回傳 - 數學知識 - 亂數 - 只有前後兩個數毫無關連才算 - 亂數表 - 如何限制亂數範圍 - `int x = rand()%6 + 1` - 餘數 - 我們可以在宣告處用運算子,也能在printf後方使用 - 一個簡單骰子(但序列固定) ```c= #include <stdio.h> #include <stdlib.h> int main() { int x; for (x = 0; x < 6; x++) { int y = rand() % 6 + 1; printf("%d\n", y); } return 0; } ``` srand(位置)改變切牌處,使每次開啟程式內容不同 例如以秒數`srand(time(NULL))` 但也有一些缺點
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up