随机数和负数 [英] Random and negative numbers

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

问题描述

我必须生成范围 [-100; +2000] .如果只有正数可用,我该如何使用rand?有什么快速的方法吗?

I have to generate numbers in range [-100; +2000] in c++. How can I do this with rand if there is only positive numbers available? Are there any fast ways?

推荐答案

生成一个介于0和2100之间的随机数,然后减去100.

generate a random number between 0 and 2100 then subtract 100.

通过Google进行的快速搜索找到了关于使用Rand()的漂亮文章.本文结尾处包含用于处理特定范围的代码示例.

A quick google search turned up a decent looking article on using Rand(). It includes code examples for working with a specific range at the end of the article.

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

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