random.h:
#ifndef __RANDOM_H__
#define __RANDOM_H__
void init_random();
int brandom(int bottom,int top);
int brandom_100(int bottom,int top);
int get_random_number(int range);
#endif // __RANDOM_H__
random.cpp:
#include "random.h"
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
void init_random() {
srand(GetTickCount());
}
int brandom(int bottom, int top) {
return (int) ((float) ((top + 1) - bottom) * rand() / (RAND_MAX + 1.0)) + bottom;
}
int brandom_100(int bottom, int top) {
Sleep(25);
return (int) ((float) ((top + 1) - bottom) * rand() / (RAND_MAX + 1.0)) + bottom;
}
int get_random_number(int range) {
return rand() % range;
}
测试main.cpp:
#include <stdio.h>
#include "random.h"
int main()
{
init_random();
printf("%d\n", brandom(10, 100));
printf("%d\n", brandom_100(10, 100));
printf("%d\n", get_random_number(10));
return 0;
}