// #include "inc-01.h"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/timeb.h>
// --------------------------------------------------------

// #include "sj-01.h"

//   skip(    3);
void skip(int no)
{
	int i;
	
	// limits no in 1 .. 20
	if ((no >= 1) && (no <= 20)) {// OK, no is OK
		for (i=1;i<=no;i++) {
			// printf("i= %2d \n", i);
			printf("\n");
		}
	}
	else {// no Good!
		// printf("\n no Good! \n");
		printf("\n");
	}
}// end of skip()
// --------------------------------------------------------

//   pause();
void pause(void)
{
	// remove type- ahead
	while (kbhit()) {
		getch();
	}
	
	printf("Press [Esc] for stop! others for Continue...");
	do {
		// wait for keyPressed
	} while (!kbhit());
	printf("\n");
	
	int ch1= getch();
	if (ch1==0x1b) {
		exit(1);
	}
	
	// remove extra keyStroke
	while (kbhit()) {
		getch();
	}
}// end of pause()
// --------------------------------------------------------


// #include "sj-02.h"
//   time1(    &t1);
void time1(int *t1)
{
	int s1, ms;
	struct _timeb timebuffer;
	_ftime( &timebuffer );
	
	s1= timebuffer.time;
	ms= timebuffer.millitm;
	
	s1%= (1L*24*60*60);
	(*t1)= s1*1000 + ms;
}// end of time1()
// --------------------------------------------------------

//   time2(t1, &dt);
void time2(int t1, double *dt)
{
	int t2;
	
	time1(&t2);
	(*dt)= (t2 - t1)/1000.0;
	
	// must (*dt) >= 0
	if ((*dt) < 0.0) {
		(*dt)+= (1.0*24*60*60);
	}
}// end of time2()
// --------------------------------------------------------



// #include "sj-03.h"
//   rnd1(    &s1);
void rnd1(int *s1)
{
	double r1= (double) (*s1);
	r1= fmod(r1*16807.0 , 2147483647.0);
	
	(*s1)= (int) (r1 + 0.5);
}// end of rnd1()
// --------------------------------------------------------

//   init_rnd(&s1);
void init_rnd(int *s1)
{
	int t1, t2, i;
	time1(&t1);
	
	t2= t1;
	while (t2 == t1) {
		time1(&t1);
	}
	// get a new t1
	
	(*s1)= t1;
	for (i=0;i<300;i++) {
		rnd1(s1);
	}
}// init_rnd()
// --------------------------------------------------------

//   rnd2(&s1, &x);
void rnd2(int *s1, double *x)
{
	rnd1(s1);
	
	(*x)= ((double) *s1)/(2147483647.0);
}// end of rnd2()
// --------------------------------------------------------

// swap2v(&i1, &i2);// swap 兩個變數的 內涵值
template <class T>
swap2v(T *a, T *b)
{
	T temp;
	
	temp= *a;
	*a= *b;
	*b= temp;
}// end of swap2v()
// --------------------------------------------------------

//   irnd(&s1, i1, i2, &ii);
void irnd(int *s1, int i1, int i2, int *ii)
{
	double x;
	
	// limits i1 <= i2
	if (i1 > i2) {
		swap2v(&i1, &i2);
	}
	
	rnd2(s1, &x);
	(*ii)= (int) (x*(i2 - i1 + 1) + i1);
}// end of irnd()
// --------------------------------------------------------

int main()
{
	int s1, i, t1, no;
	double dt;
	no= (int) (1.0E8 + 0.5);
	
	time1(&t1);
	s1= t1;
	for (i=0;i<no;i++) {
		rnd1(&s1);
	}
	time2(t1, &dt);
	
	skip(3);
	printf("dt= %.3lf, no/dt= %.6le \n", dt, no/dt);
	pause();
	return(0);
}// end of main()
