请教: vc 中的如何取随机数的问题 ,
如我想在1-10 之间取随机数. 且精确到2位小数...
应如何解决.........
请高手指点...
解决后马上结贴!!!!!!!!!
如我想在1-10 之间取随机数. 且精确到2位小数...
应如何解决.........
请高手指点...
解决后马上结贴!!!!!!!!!
解决方案 »
- 如何修改dlg的大小
- CListBox控件如何设置才能实现一行存两个列的数据?
- VC里用ADO一次记读取100万条记录速度慢
- 请问大哥哥CRichEditCtrl与CEdit 有什么区别?
- 如何为已经创立好的某个菜单的某个选项(如“文件”)添加一个子项,并且如何响应这个子项的单击消息?
- 那里有 魔戒 2 免费下载?
- 哪位老兄有《windows 2000环境下visual c++编程》(英文:《Mastering Windows 2000 Programming With Visual C++》)的随书光盘例子??
- 怎样得到字符串的。。
- vc的显示真是头痛
- 如何删除注册表中的一个键值??????急死我了!!
- 急问:关于创建一个系统枚举组件的问题。
- 数据类型转换问题!!求助!!
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_rand.asp
rand ();
即可首先设置一个种子,种子可以为任意数,然后使用rand获得一个random数
多谢了!
int value = rand() % 1000; //如果可以取到10.xx,那么就是%1100,返回不大于1100的非负整数
float dest = value / 100.0;
Example
// crt_rand.c
/* This program seeds the random-number generator
* with the time, then displays 10 random integers.
*/#include <stdlib.h>
#include <stdio.h>
#include <time.h>int main( void )
{
int i; /* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) ); /* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
}
int i=rand()%10+1;
{ double i=100+rand()%900;
m_result.Format("%.2f",i/100.);
UpdateData(FALSE);
}
int i=rand()%10000+1;//1-10000的随机数
float d=i/100.00;//根据自己的随机数位数决定
CString str;
str.Format("%.2f",d);//精确到两位
AfxMessageBox(str);