随机数发生器 [英] Random Number Generator

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

问题描述

我需要用Java编写程序,使用以下公式生成[0,1]范围内的随机数:

I need to write a program in Java to generate random numbers within the range [0,1] using the formula:


X i =(aX i-1 + b)mod m

Xi = (aXi-1 + b) mod m

假设a,b&的任何固定int值m和X 0 = 0.5(即i = 0)

assuming any fixed int values of a, b & m and X0 = 0.5 (ie i=0)

我该怎么做?

我试过这样做但显然是错误的:

i tried doing this but it's obviously wrong:

int a = 25173, b = 13849, m = 32768;
double X_[i];
for (int i = 1; i<100; i++)
   X_[i] = (a*(X_[i]-1) + b) % m;
double X_[0] = 0.5;
double double = new double();
System.out.println [new double];


推荐答案

以下是一些提示:

int a, d, m, x;

乘法是 * mod

好的,我会再给你一点提示。你只需要一个X,你不需要所有这些数组;因为你只使用整数,所以你不需要任何浮点数或双重数。

Okay, I'll give you a little more of a hint. You only need one X, you don't need all these arrays; since you're only using integers you don't need any floats or doublts.

重要的代码行将是

x = (a * x + b) % m ;

你不需要另外的 x 那里因为 = 右侧的 x 是OLD x ,或 x i-1 ;左侧的那个将是您的新 x ,或 x i

You don't need another x there because the x on the right hand side of the = is the OLD x, or xi-1; the one on the left side will be your "new" x, or xi.

现在,你需要编写一个Java包装器,它可以让你创建一个方法,这意味着编写一个

Now, from there, you need to write the Java wrapper that will let you make that a method, which means writing a class.

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

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