如何在两个数字之间生成随机数 [英] How to generate random number between two numbers

查看:409
本文介绍了如何在两个数字之间生成随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两个数字x和y之间生成一个randowm数字,即


int randomNoBetween(int x,int y);


Plz帮助

有没有这样的功能?

I want to generate a randowm number between two numbers x and y i.e

int randomNoBetween(int x, int y);

Plz help
Is there any such function??

推荐答案

Sanchit写道:
Sanchit wrote:

我想在两个数字x和y之间生成一个randowm数字,即


int randomNoBetween(int x,int y);
I want to generate a randowm number between two numbers x and y i.e

int randomNoBetween(int x, int y);



(我猜)在区间[0,n]中获取随机数的最简单方法

(即0 ,n独家)是(如果n足够小)可能

(rand()%n),但这会略微偏向较小的数字。


我不知道是否有任何共识关于是否使用这种方法或

一些更昂贵的(如浮点运算)来做这个。


使用这种方法,写你的是很简单的有问题的功能;这个

留给你练习;)


Daniel


-

完成:Arc-Bar-Cav-Rog-Sam-Val-Wiz

去:Hea-Kni-Mon-Pri-Ran-Tou

The (I guess) simplest way to get a random number in the interval [0, n)
(that is, 0 inclusive, n exclusive) is (if n is small enough) probably
(rand() % n), which will however slightly favor smaller numbers.

I can''t tell if there''s any "consensus" on whether to use this method or
some more expensive (like floating point arithmetic) to do this.

With this method, it is trivial to write your function in question; this
is left as an exercise to you ;)

Daniel

--
Done: Arc-Bar-Cav-Rog-Sam-Val-Wiz
To go: Hea-Kni-Mon-Pri-Ran-Tou


9月29日下午2:43,Daniel Kraft< d ... @ domob.euwrote:
On Sep 29, 2:43 pm, Daniel Kraft <d...@domob.euwrote:

Sanchit写道:
Sanchit wrote:

我想在两个数字x和y之间生成一个randowm数,即
I want to generate a randowm number between two numbers x and y i.e


int randomNoBetween(int x,int y);
int randomNoBetween(int x, int y);



参见C-FAQ的问题13.16。

< http://c-faq.com/> ;

See question 13.16 of the C-FAQ.
<http://c-faq.com/>


(我猜)在区间[0,n]中获取随机数的最简单方法

(即0包含,n独家)是(如果n足够小)可能是

(rand()%n),但这会略微偏向较小的数字。
The (I guess) simplest way to get a random number in the interval [0, n)
(that is, 0 inclusive, n exclusive) is (if n is small enough) probably
(rand() % n), which will however slightly favor smaller numbers.



是的,请参阅上述常见问题解答的问题13.18以了解原因。

Yes, see question 13.18 of the aforementioned FAQ to learn why.


9月29日,7: 43 * am,Daniel Kraft< d ... @ domob.euwrote:
On Sep 29, 7:43*am, Daniel Kraft <d...@domob.euwrote:

Sanchit写道:
Sanchit wrote:

我想在两个数字x和y之间生成一个randowm数,即
I want to generate a randowm number between two numbers x and y i.e


int randomNoBetween(int x,int y);
int randomNoBetween(int x, int y);



(我猜)在区间[0,n]中获取随机数的最简单方法

(即0,包括0) ,n独家)是(如果n足够小)可能

(rand()%n),但这会略微偏向较小的数字。


The (I guess) simplest way to get a random number in the interval [0, n)
(that is, 0 inclusive, n exclusive) is (if n is small enough) probably
(rand() % n), which will however slightly favor smaller numbers.



你可以通过计算

RAND_MAX的最大倍数来消除偏差,它可以被n整除。打电话给M.然后直接扔掉

随机数,直到你得到一个0到M-1的范围。致电

这个R.然后返回R%n。

You can eliminate the bias by computing the largest multiple of
RAND_MAX that''s evenly divisible by n. Call this M. Then just throw
away random numbers until you get one in the range 0 to M-1. Call
this R. Then return R % n.


这篇关于如何在两个数字之间生成随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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