Random.cpp
资源名称:realtime.rar [点击查看]
上传用户:mgf822
上传日期:2013-10-03
资源大小:133k
文件大小:2k
源码类别:
对话框与窗口
开发平台:
Visual C++
- #include <malloc.h>
- #include "random.h"
- RndGen::~RndGen()
- {
- if (oldrand)
- free(oldrand);
- }
- void RndGen::init_random(const float randomseed)
- {
- float new_random = 1.0e-9f;
- float prev_random = randomseed;
- oldrand = (float*)calloc(55, sizeof(float));
- oldrand[54] = randomseed;
- for (int i = 0; i < 53; i ++)
- {
- int j = (21 * (i + 1)) % 55;
- oldrand[j] = new_random;
- new_random = prev_random - new_random;
- if (new_random < 0) new_random ++;
- prev_random = oldrand[j];
- }
- advance_random();
- advance_random();
- advance_random();
- n = 0;
- }
- void RndGen::advance_random()
- {
- float new_random;
- for (int i = 0; i <= 23; i ++)
- {
- new_random = oldrand[i] - oldrand[i+31];
- if (new_random < 0)
- new_random ++;
- oldrand[i] = new_random;
- }
- for (i = 24; i <= 54; i ++)
- {
- new_random = oldrand[i] - oldrand[i-24];
- if (new_random < 0)
- new_random ++;
- oldrand[i] = new_random;
- }
- }
- float RndGen::randm()
- {
- n ++;
- if (n > 54)
- {
- n = 1;
- advance_random();
- }
- return oldrand[n];
- }