在C51下的随机数生成

/* --------------------------------------------*    描述 :        在 Windows server 2003, vs.net 2003下调试通过
*
*    作者 : e.Studio·zhzkl
*    参考 : rfc4122
* -------------------------------------------
*/

#include 
<stdio.h>
#include 
<stdlib.h>
#include 
<conio.h>

#define A 3

int AdvancedLinearCongrutial(int x, int a, int b, int c, int m, int max);


int main(int argc, char* argv[])
{
    
char* nums = argv[1];    // 取得参数
    char mynums[10];    // 预定义10位长度的字符数组, 单片机为8位  应该不会超过这个范围了
    int res = 0, pos = 0, length = 0;

    
// 将字符流转换为Int
    
//
    while(nums[pos] != '\0' )
    
{
        length 
= length + 1;
        pos 
= pos + 1;
    }

    pos 
= 0;
    length 
= length - 1;

    
while(nums[pos] != '\0')
    
{
        mynums[length 
- pos] = nums[pos];
        pos
++;
    }


    pos 
= 0;
    
while(nums[pos] != '\0')
    
{
        
int m = 1;
        
for(int k = 0; k<pos; k++)
        
{
            m 
= m * 10;        
        }


        res 
= res + (mynums[pos] - 48* m;
        pos 
= pos + 1;
    }


    
// 定义随机种子
    
//
    srand(res);
    
int x = rand(), a = 2, b = 3, c = 5, m = 65536;
    
    
// 改进的线性同余算法
    int i = AdvancedLinearCongrutial(x, a, b, c, 65536, rand()%100);
    printf(
"i = %d", i);
    printf(
"\tres = %d", res);
    
return i;
}


// 改进的线性同余算法
int AdvancedLinearCongrutial(int x, int a, int b, int c, int m, int max)
{
    
for(int i=0; i<max; i++)
    
{
        a 
= A;
        
for(int j=0; j<i; j++)
        
{            
            a 
*= A;
        }

        x 
= (a*+ (a-1)*c/b)%m;
    }


    
return x;
}

posted on   xwang  阅读(3326)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述

导航

< 2006年5月 >
30 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 1 2 3
4 5 6 7 8 9 10

统计

点击右上角即可分享
微信分享提示