宁静致远

------------------------------------------------------------------------------------

VC知识库BLOG 首页 新随笔 联系 聚合 登录
  45 Posts :: 0 Stories :: 112 Comments :: 1 Trackbacks

公告

-----000来了哈000-----

留言簿(0)

随笔分类

随笔档案

文章档案

相册

技术FAQ

搜索

最新评论

阅读排行榜

评论排行榜

// Rand.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(int argc, char* argv[])
{
 int i,j;
 
 srand( (unsigned)time( NULL ) );
 int total[2] = {0};
 int temp = 0;
 
 for( i = 0;   i < 1000000;i++ )
 {
  temp = rand()%2;
  printf( "  %d\n",temp);
  total[temp]++;
 }
 
 printf("############################################################################\n");

 for(j = 0; j < 2; j++)
 {
  printf("total[%d]=%d\n",j,total[j]);
 }
 
 system("pause");
 
 return 0;
}

/*
结果:

第一组:
total[0]=500026
total[1]=499974
第二组:
total[0]=500159
total[1]=499841
第三组:
total[0]=500023
total[1]=499977
第四组:
total[0]=500034
total[1]=499966
第五组:
total[0]=499962
total[1]=500038
第六组:
total[0]=499898
total[1]=500102
第七组:
total[0]=500002
total[1]=499998
第八组:
total[0]=499991
total[1]=500009
第九组:
total[0]=500106
total[1]=499894
第十组:
total[0]=499997
total[1]=500003
*/

posted on 2007-08-21 23:12 blog of vc_student 阅读(2979) 评论(5)  编辑 收藏

Feedback

# re: 生成随机数测试代码 2007-08-22 09:01 Diviner
这些数字似乎不随机。

# 我也怀疑中,几率好像还可以,但也让人心存疑虑!50%! 2007-08-22 10:33 三年级小学生
rt

# re: 生成随机数测试代码 2007-08-22 12:26 Diviner
int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
if(!CryptAcquireContext(&os->handle, 0, 0, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
return WINCRYPT_E;

if (!CryptGenRandom(os->handle, sz, output))
return CRYPTGEN_E;

CryptReleaseContext(os->handle, 0);

return 0;
}


int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
if(!CryptAcquireContext(&os->handle, 0, 0, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
return WINCRYPT_E;

if (!CryptGenRandom(os->handle, sz, output))
return CRYPTGEN_E;

CryptReleaseContext(os->handle, 0);

return 0;
}

# 什么东西哦?片断 2007-08-23 18:50 小学生
这段代码看不懂哦!干什么用的?

# re: 生成随机数测试代码 2007-09-03 12:59 maggie
vc资料站:http://www.vcmsdn.com/     对学习很有帮助的,可以上去

看看,或加群46138350,里面有高手可以请教的。

标题  
姓名  
主页
验证码 *
内容   
  登录  使用高级评论  Top
[使用Ctrl+Enter键可以直接提交]