随机数 - 为什么这段代码不起作用? [英] Random Numbers -- why doesn't this piece of code work?

查看:114
本文介绍了随机数 - 为什么这段代码不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的程序员,


看一下这个函数,生成从1到数的随机数。

它根本不起作用。好吧,实际上它默默地编译,但是然后

返回1 (编译:​​gcc / ++ 3.3.2-r5; Gentoo Linux)。


#include< cstdlib> //对于随机数生成器

#include< ctime> //对于时间函数

使用命名空间std;


inline int random(int count){

return 1 +

static_cast< int>((count * static_cast< long>(rand()))/(RAND_MAX + 1));

}


我能够发现命令_will_按预期工作

没有RAND_MAX,但显然没有任何意义,那么。


提前致谢,

Moritz Beller

-

web http://www.4momo.de

邮件momo.beller at t-online dot de

Dear programmers,

Have a look at this function to generate random numbers from 1 to count.
It simply does not work. Well, actually it compiles silently, but then
returns "1" (Compiler: gcc/++ 3.3.2-r5; Gentoo Linux).

#include <cstdlib> // For random number generator
#include <ctime> // For time function
using namespace std;

inline int random(int count) {
return 1 +
static_cast<int>((count*static_cast<long>(rand()))/(RAND_MAX+1));
}

I was able to find out that the command _will_ work as it is expected
with out RAND_MAX, but obviously doesn''t make any sense, then.

Thanks in advance,
Moritz Beller
--
web http://www.4momo.de
mail momo.beller at t-online dot de

推荐答案

2004年6月3日星期四19:19:22 +0200,Moritz Beller

< mo ******** *********@t-online.de>写道:
On Thu, 3 Jun 2004 19:19:22 +0200, Moritz Beller
<mo*****************@t-online.de> wrote:
亲爱的程序员,

看看这个函数生成从1到数的随机数。
它根本不起作用。好吧,实际上它是静默编译的,但是然后
返回1。 (编译:​​gcc / ++ 3.3.2-r5; Gentoo Linux)。

#include< cstdlib> //对于随机数生成器
#include< ctime> //对于时间函数
使用命名空间std;

inline int random(int count){
返回1 +
static_cast< int>((count * static_cast< long>(rand()))/(RAND_MAX + 1));
}

我能够发现命令_will_按照预期的方式工作
RAND_MAX,但显然没有任何意义。

提前致谢,莫里茨贝勒
Dear programmers,

Have a look at this function to generate random numbers from 1 to count.
It simply does not work. Well, actually it compiles silently, but then
returns "1" (Compiler: gcc/++ 3.3.2-r5; Gentoo Linux).

#include <cstdlib> // For random number generator
#include <ctime> // For time function
using namespace std;

inline int random(int count) {
return 1 +
static_cast<int>((count*static_cast<long>(rand()) )/(RAND_MAX+1));
}

I was able to find out that the command _will_ work as it is expected
with out RAND_MAX, but obviously doesn''t make any sense, then.

Thanks in advance,
Moritz Beller




我的避风港不考虑你的逻辑,因为,给定:


#include< iostream>

#include< cstdlib> //对于随机数生成器

#include< ctime> //对于时间函数

使用std :: cout;

使用std :: endl;


inline int random(int计数){

返回1 +


static_cast< int>((计数* static_cast< long>(rand()))/(RAND_MAX + 1) );

}


int main()

{

srand(time(0) );

cout<<随机(10)<< endl;

cout<< random(100)<< endl;

cout<<随机(1000)<< endl;

cout<<随机(1000)<<结束;


返回0;

}

这是'发生的事情:


d:\ src \ learn> g ++ - version

g ++(GCC)3.2.3(mingw special 20030504-1)

版权所有(C)2002自由软件基金会,公司

这是免费软件;查看复制条件的来源。没有

保修;甚至不适用于适销性或特定用途的适用性。


d:\ src \ learn> g ++ rand.cpp


d:\ src\learn> rand

5

49

552

60


(当然,数字会有所变化)


所以我不想去寻找bug。还挺。你在测试程序开始时做了一个

srand()吗?

-leor


-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html


" Moritz Beller" <莫***************** @ t-online.de>在消息中写道

news:c9 ************* @ news.t-online.com ...
"Moritz Beller" <mo*****************@t-online.de> wrote in message
news:c9*************@news.t-online.com...
亲爱的程序员,

看看这个函数生成从1到数的随机数。
它根本不起作用。好吧,实际上它是静默编译的,但是然后
返回1。 (编译:​​gcc / ++ 3.3.2-r5; Gentoo Linux)。

#include< cstdlib> //对于随机数生成器
#include< ctime> //对于时间函数
使用命名空间std;

inline int random(int count){
返回1 +
static_cast< int>((count * static_cast< long>(rand()))/(RAND_MAX + 1));
}

我能够发现命令_will_按照预期的方式工作
RAND_MAX,但显然没有任何意义。

提前致谢,
Moritz Beller
Dear programmers,

Have a look at this function to generate random numbers from 1 to count.
It simply does not work. Well, actually it compiles silently, but then
returns "1" (Compiler: gcc/++ 3.3.2-r5; Gentoo Linux).

#include <cstdlib> // For random number generator
#include <ctime> // For time function
using namespace std;

inline int random(int count) {
return 1 +
static_cast<int>((count*static_cast<long>(rand()))/(RAND_MAX+1));
}

I was able to find out that the command _will_ work as it is expected
with out RAND_MAX, but obviously doesn''t make any sense, then.

Thanks in advance,
Moritz Beller




你' '将该分裂的结果转换为长,但是分区

本身会产生至少为0但小于1的值。所以,将其转换为

a long将它截断为0,总是!我想如果你把演员阵容移到很久,

你会得到更好的结果。


-Howard


所有程序员都写出完美的代码。

....所有其他程序员都写垃圾。


我永远不会错了。

我以为我错了一次,

但我错了。



You''re casting the result of that division to a long, but the division
itself results in a value of at least 0 but less than 1. So, casting it to
a long truncates it to 0, always! I think if you remove the cast to long,
you''ll get a better result.

-Howard

"All programmers write perfect code.
....All other programmers write crap."

"I''m never wrong.
I thought I was wrong once,
but I was mistaken."


Moritz Beller写道:
Moritz Beller wrote:
亲爱的程序员,

看看这个函数生成从1到数的随机数。
它根本不起作用。好吧,实际上它是静默编译的,但是然后
返回1。 (编译:​​gcc / ++ 3.3.2-r5; Gentoo Linux)。


您希望它返回什么?

#include< cstdlib> //对于随机数生成器
#include< ctime> //对于时间函数
使用命名空间std;

inline int random(int count){
返回1 +
static_cast< int>((count * static_cast< long>(rand()))/(RAND_MAX + 1));
}

我能够发现命令_will_按照预期的方式工作
RAND_MAX,但显然没有任何意义。那么。
Dear programmers,

Have a look at this function to generate random numbers from 1 to count.
It simply does not work. Well, actually it compiles silently, but then
returns "1" (Compiler: gcc/++ 3.3.2-r5; Gentoo Linux).
What do you expect it to return?

#include <cstdlib> // For random number generator
#include <ctime> // For time function
using namespace std;

inline int random(int count) {
return 1 +
static_cast<int>((count*static_cast<long>(rand()))/(RAND_MAX+1));
}

I was able to find out that the command _will_ work as it is expected
with out RAND_MAX, but obviously doesn''t make any sense, then.




在''rand''的实现中它总是可以返回0
如果未播种。此外,您将始终获得相同的伪随机数序列。你需要使用''srand''。


V



It is possible that in your implementation of ''rand'' it always returns 0
if unseeded. Besides, you will always get the same sequence of pseudo-
random numbers. You need to use ''srand''.

V


这篇关于随机数 - 为什么这段代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆