简体   繁体   English

使用从奇数流中取模来生成“随机”数

[英]generating 'random' number using modulo from a stream of odd numbers

i want to generate a pseudo-random bool stream based on a modulo operation on another stream of integers (say X), so the operation would be 我想基于对另一个整数流(例如X)的模运算来生成伪随机布尔流,因此该运算将是

return ( X % 2);

The only problem is that X is a stream of integers that always ends in 1, so for instance would be somehing like 1211, 1221, 1231, 1241 .... is there a way for me to disregard the last bit (without using string manip) so the test doesnt always pass or always fail? 唯一的问题是X是一个总是以1结尾的整数流,因此例如1211、1221、1231、1241等。我有办法忽略最后一位(不使用字符串) manip),因此测试不是总是通过或总是失败?

那么(X / 10) % 2呢?

If you'd otherwise be happy to use the last bits, use the penultimate bits instead: 如果您不满意使用最后几位,请改用倒数第二位:

return (x & 0x2) >> 1;

So say the next number from your stream is 23: 假设您的信息流中的下一个数字是23:

  1 0 1 1 1  // 23 in binary
& 0 0 0 1 0  // 0x2 in binary
-----------
  0 0 0 1 0

Shifting that right by one bit ( >> 1 ) gives 1. With 25, the answer would be 0: 向右移动一位( >> 1 )得到1。得到25,答案将为0:

  1 1 0 0 1
& 0 0 0 1 0
-----------
  0 0 0 0 0
return ( x%20/10 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM