兰德()与基地 [英] Rand() with base

查看:64
本文介绍了兰德()与基地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想用rand生成一个随机的8位数字(0

是可能的吗?要特别指出基数和长度?

谢谢

Hi,
I want to generate a random 8 bit number using rand(0
is that possible? to expecifu the base and the lenght?
thanks

推荐答案

Profetas< xu ***** @ yahoo.com>潦草地写道:
Profetas <xu*****@yahoo.com> scribbled the following:

我想使用rand生成一个随机的8位数字(0
可能吗?要特别指出基数和长度?
谢谢
Hi,
I want to generate a random 8 bit number using rand(0
is that possible? to expecifu the base and the lenght?
thanks




Base和length不适用于数字,只适用于
的表示。因此,您以相同的方式生成8位数字你生成

任何其他数字。

现在一个8位数字的值从0到255.因此你只需要

" cut rand()以两种方式之一返回的数字:你可以

将其模数为256(%256)或按位 - 并且它为255(& 255)。


-

/ - Joona Palaste(pa*****@cc.helsin ki.fi)-------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste --------------- ------规则! -------- /

在狗的外面,一本书是男人最好的朋友。无论如何,在狗的内心,它太黑了



- Groucho Marx



"Base" and "length" do not apply to numbers, only representations of
them. Therefore you generate an 8 bit number the same way you generate
any other number.
Now an 8 bit number has values from 0 to 255. Therefore you only need to
"cut down" the number returned by rand() in either of two ways: you can
modulo it by 256 (%256) or bitwise-AND it with 255 (&255).

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Outside of a dog, a book is a man''s best friend. Inside a dog, it''s too dark
to read anyway."
- Groucho Marx


我需要像1101011010101010这样的东西。

随机自动生成。

如何指定rand()限制?例如我想要一个随机数

10?

这样我可以说我想要一个随机数01.
I need something like 1101011010101010.
to be generated randomly and automatically.
How do I specify the rand() limit? for example I want a random number of
10?
this way I can say I want a random number of 01.

Profetas< xu ***** @ yahoo.com>潦草地写道:
Profetas <xu*****@yahoo.com> scribbled the following:
我需要像1101011010101010这样的东西。


那不是8位数。无论如何,你需要明白,当你打印

数字或者读取它们时,基数之间的差异才会发挥作用。在内部,数字是一个数字是一个

的数字。

随机自动生成。
如何指定rand()限制?比如我想要一个随机数的10个?


我刚刚告诉过你。 rand()函数返回一个从0到RAND_MAX的值。

使用%或&操作员将其保持在你想要的范围内。

当你说10的随机数时,你的意思是10或10的二进制,

即2 ?如果它是前者,那么你可以使用rand()%10。如果它是

后者,你可以使用rand()%2或rand()& 1.

这样我可以说我想要一个随机数01.
I need something like 1101011010101010.
That''s not an 8 bit number. Anyway, you need to understand that the
difference between bases only comes into play when you are printing
numbers out, or reading them in. Internally, a number is a number is a
number.
to be generated randomly and automatically.
How do I specify the rand() limit? for example I want a random number of
10?
I just told you. The rand() function returns a value from 0 to RAND_MAX.
Use the % or & operators to keep it within the range you want.
When you say "a random number of 10", do you mean ten, or 10 in binary,
i.e. two? If it''s the former, then you can use rand()%10. If it''s the
latter, you can use rand()%2 or rand()&1.
this way I can say I want a random number of 01.




-

/ - Joona Palaste(pa*****@cc.helsinki.fi) - -----------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /

"''所谓的''意味着'''这有很长的解释,但我没有

时间在这里解释一下。''"

- JIPsoft



--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"''So called'' means: ''There is a long explanation for this, but I have no
time to explain it here.''"
- JIPsoft


这篇关于兰德()与基地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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