Re: [問題] 產生超過32767的亂數

看板C_and_CPP (C/C++)作者 (sjgau)時間14年前 (2012/05/15 19:39), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串3/4 (看更多)
#include <cstdlib> #include <iostream> using namespace std; // ---------------------------------------------- #include <math.h> // ---------------------------------------------- // rnd1( &s1); void rnd1(int *s1) { double x1, x2, x3; x1= (double) (*s1); x2= x1*16807.0; x3= fmod(x2, 2147483647.0); (*s1)= (int) (x3 + 0.5); }// end of rnd1() // ---------------------------------------------- void init_rnd(int *ss) { int s1, s2, ct1; s1= time(NULL); s2= s1; ct1= 0; while (s1 == s2) { // get new s1 s1= time(NULL); ct1++; } // s1 <> s2 *ss= (s1 + ct1); }// end of init_rnd() // ---------------------------------------------- int main(int argc, char *argv[]) { int s1, i; init_rnd(&s1); printf("\n s1= %d \n", s1); for (i=1;i<=20;i++) { rnd1(&s1); printf("s1= %12ld \n", s1); } system("pause"); return EXIT_SUCCESS; }// end of main() -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.193.224.204

05/15 21:08, , 1F
謝謝幫忙^_^
05/15 21:08, 1F

05/16 10:08, , 2F
不客氣!針對這個議題,討論的空間 還很大!
05/16 10:08, 2F
文章代碼(AID): #1Fia1Ruu (C_and_CPP)
文章代碼(AID): #1Fia1Ruu (C_and_CPP)