随机数发生器.... [英] Random Number Generators....

查看:76
本文介绍了随机数发生器....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我根本不理解的问题。在我之前的

帖子中,我可以开始我的项目,我只是遇到了一些问题

语法错误。这个问题是我在概念上无法理解的事情。这是:


?* a ??圆周长与直径之比

a ??是数学中最常见和最重要的常数之一。

这是一个无理数(一个实数不能表示为两个整数的比率),但是它的价值计算得比使用不同公式的一百万个小数位数更多。 (参见

例子,Petr Beckmann的历史?*,圣马丁出版社:纽约,

1971)在这个任务中你通过模拟随机投掷飞镖的目标来估算?*的价值。


左边的数字是半径为1.0的四分之一圆圈
笛卡尔飞机上1.0美元/平方英尺的1.0平方。可以看出深蓝色四分之一圆的面积是?* / 4。如果我们在广场上随机扔d />
飞镖,并且它们的c落在圆圈中,我们可以将b * b估计为* * / 4的值为c / d,所以近似值* *是4 *(

c / d)。


你的程序应该抛出通过生成一个

随机坐标对(x,y)来对目标进行飞镖,其中x和y是浮点值

,范围为[0.0,1.0],这将是代表飞镖击中

目标。 (您可以使用表达式

(double)rand()/ RAND_MAX)生成此范围内的数字。)如果出现以下情况,此点将在季度内部

圈内:


sqrt(x2 + y2)< = 1.0


通过保持计数器登陆的飞镖数量

四分之一圈和投掷的飞镖总数,值可以近似值为
?*。抛出的飞镖数量越多,近似值就越好。


你的程序应该使用一个函数,每次调用时,

生成一个新的随机坐标对,确定这一对

是否代表四分之一圆圈内的一个点,如果是,则返回1,

如果不是,则为0。你的程序应该运行模拟一个用户指定的飞镖投掷次数,然后显示估计的?*
值?*和百分比差异(百分比误差)你的
估计值和真实价值?*;如果您的估计值是p,

那么百分比误差是((p - ?*)/?*)* 100.(实际值

?*来15个有效数字是3.14159265358979。)


__________________________________________________ _________________________


我确实对我需要的变量有一些想法,但我不是确定

如何真正实现rand()/ RAND_MAX功能......有人可以给某个人开始正确的方向吗?

I have a problem that I really don''t understand at all. In my previous
post I could get started on my projects I just had a few problems with
syntax errors. This problem is something that I don''t conceptually
understand very well. Here it is:

?* a?? the ratio of the circumference of a circle to its diameter
a?? is one of the most common and important constants in mathematics.
It is an irrational number (a real number that cannot be expressed as
the ratio of two integers), but its value has been calculated to more
than a million decimal places using different formulas. (See, for
example, Petr Beckmann''s A History of ?*, St. Martin''s Press: New York,
1971) In this assignment you will estimate the value of ?* by
simulating randomly throwing darts at a target.

The figure at the left is a quarter circle of radius 1.0 inscribed in
a square of side 1.0 on the Cartesian plane. It can be show that area
of the dark blue quarter circle is ?*/4. If we were to randomly throw d
darts at the square, and c of them landed in the circle, we could
estimate the value of ?*/4 as c / d, so an approximation of ?* is 4* (
c / d ).

Your program should "throw" darts at the target by generating a
random coordinate pair (x, y) where x and y are floating point values
in the range [0.0, 1.0], which will represent the point the dart hit
the target. (You can generate numbers in this range with the expression
(double)rand()/RAND_MAX) .) This point will be inside the quarter
circle if:

sqrt( x2 + y2) <= 1.0

By maintaining counters for the number of darts landing in the
quarter circle and the the total number of darts thrown, the value of
?* can be approximated. The larger the number of darts thrown, the
better the approximation will be.

Your program should use a function that, each time it is called,
generates a new random coordinate pair, determines if this pair
represents a point inside the quarter circle, and returns a 1 if it is,
and a 0 if it is not. Your program should run a simulation for a
user-specified number of dart throws, and then display the estimated
value of ?* and the percentage difference (percent error) between your
estimated value and the true value of ?*; if your estimated value is p,
then the percent error is (( p - ?*) / ?* ) * 100. (The actual value of
?* to 15 significant figures is 3.14159265358979.)

__________________________________________________ _________________________

I do have some ideas about the variables that I need but I''m not sure
how to really implement the rand()/ RAND_MAX function... could someone
give a start in the right direction?

推荐答案

RadiationX写道:
RadiationX wrote:
[...]

你的程序应该抛出通过生成随机坐标对(x,y)在目标上飞镖,其中x和y是[0.0,1.0]范围内的浮点值,这将代表飞镖击中的点(double)rand()/ RAND_MAX)[...]

我对我需要的变量有一些想法,但我''我不确定
如何真正实现rand()/ RAND_MAX功能......有人可以在正确的方向上开始吗?
[...]

Your program should "throw" darts at the target by generating a
random coordinate pair (x, y) where x and y are floating point values
in the range [0.0, 1.0], which will represent the point the dart hit
the target. (You can generate numbers in this range with the expression
(double)rand()/RAND_MAX) [...]

I do have some ideas about the variables that I need but I''m not sure
how to really implement the rand()/ RAND_MAX function... could someone
give a start in the right direction?




您不需要实现rand()函数也不需要
定义RAND_MAX;它们是标准C库的一部分。

只需#include< stdlib.h>并开始使用它们。


-

Eric Sosman
es ***** @ acm-dot-org.inva lid



You do not need to implement the rand() function nor
define RAND_MAX; they are part of the Standard C library.
Just #include <stdlib.h> and start using them.

--
Eric Sosman
es*****@acm-dot-org.invalid


RadiationX写道:
RadiationX wrote:
我确实对我需要的变量有一些想法,但我不确定如何真正实现rand()/ RAND_MAX功能......有人可以在正确的方向上开始吗? ?
I do have some ideas about the variables that I need but I''m not sure
how to really implement the rand()/ RAND_MAX function... could someone
give a start in the right direction?




rand()和相关的常量RAND_MAX都是内置C

API的一部分。


Uli



Both rand() and the associated constant RAND_MAX are part of the builtin C
API.

Uli


文章< 11 ********************* @ u72g2000cwu.googlegroups。 com>,

RadiationX< he ********* @ gmail.com>写道:
In article <11*********************@u72g2000cwu.googlegroups. com>,
RadiationX <he*********@gmail.com> wrote:
你的程序应该使用一个函数,每次调用它时,
生成一个新的随机坐标对,确定这对是否代表四分之一圈内的一个点,如果是,则返回1,
如果不是则返回0。
Your program should use a function that, each time it is called,
generates a new random coordinate pair, determines if this pair
represents a point inside the quarter circle, and returns a 1 if it is,
and a 0 if it is not.




小心。 A -lot-实现对rand()使用

线性同余随机数生成器,并且

如果你使用
那就有一个众所周知的问题
来自流的结果样本为(x,y)对和图表

结果,而不是均匀地填充一个正方形,你会得到

a的数量明显分离的诊断线。


可以使用各种修正来减少这个问题,但最好的修正是使用更好的随机数

发电机。一个据说速度很快且非常好的是Mersenne Twister,
http://www.math.sci.hiroshima-u.ac.j...at/MT/emt.html

-

一切都是虚荣心。 - 传道书



Be careful with that. A -lot- of implementations use
linear congruential random number generators for rand(), and
there is a well known problem with those that if you use
consequative samples from the stream as (x,y) pairs and graph
the results, instead of filling a square evenly, you will get
a number of noticably seperated diagnonal lines.

There are various corrections that one can use to reduce this
problem, but the best correction is to use a much better random number
generator. One that is said to be fast and very good is the
Mersenne Twister,
http://www.math.sci.hiroshima-u.ac.j...at/MT/emt.html
--
All is vanity. -- Ecclesiastes


这篇关于随机数发生器....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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