Re: [問題] 產生超過32767的亂數
#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
討論串 (同標題文章)
完整討論串 (本文為第 3 之 4 篇):
12
75
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章