C#中的函数Rand_MAX [英] function Rand_MAX in C#

查看:251
本文介绍了C#中的函数Rand_MAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好J的蒙特卡洛方法RAND_MAX有问题. J不知道该如何解决

Hi J have got a problem with monte carlo method RAND_MAX. J don''t know ow to resolve it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public static class GlobalMembersC

public static int Main(){
    int n = 100000;
    double xmax = 0.0;
    double fmax = 0.0;
    
    for (int i = 0; i < n; i++)
    {
        double x = Math.PI * RandomNumbers.NextNumber()/(RAND_MAX+1.0);

        double f =  Math.Sin(x * x * x)),2);
   
        if (f > fmax)
        {
            fmax = f;
                }
    }

    Console.Write("f=");
    Console.Write(fmax);




[摘自解决方案"]
J要计算函数Math.Sin(x * x * x)),2)在C#环境中的蒙特卡洛方法的最小值,RAND_MAX是上限,但j有一个错误:错误名称``RAND_MAX''不存在在当前情况下

另一个问题是错误:




[From "solution"]
J want to calculate min of function Math.Sin(x * x * x)),2) monte carlo methods in C# environment, RAND_MAX is the upper limit but j have a mistake:Error The name ''RAND_MAX'' does not exist in the current context

Another problem is error:

Error   1   'GlobalMembersC.Main()': not all code paths return a value

推荐答案

RAND_MAX似乎是代码中其他地方定义的常量.它不应该是函数名称,因此实际上没有任何实现.

不知道RAND_MAX应该代表什么,就不可能告诉您它应该是什么.您是从哪儿来的?A)从哪里获得此代码,或B)从哪里得到编写此代码的信息? RAND_MAX将在您获取/编写此代码的原始源信息中进行定义.
RAND_MAX appears to be a constant defined somewhere else in the code. It''s should not be a function name, so there really isn''t anything to implement.

Without knowing what RAND_MAX is supposed to represent, it''s impossible to tell you what it should be. Where did you either A) Get this code from, or B) Get the information to write this code from?? RAND_MAX will be defined in the original source information you got/wrote this code from.


您可以尝试替换该行
You may try to replace the line
double x = Math.PI * RandomNumbers.NextNumber()/(RAND_MAX+1.0);






with

double x = Math.PI * RandomNumbers.NextNumber()/Int32.MaxValue;



(尽管文档 [



(though the documentation[^] is a bit vague on the maximum value of the Random.Next() generated number).


这篇关于C#中的函数Rand_MAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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