Random 让一个数字在某个百分比的时间内显示

Random 让一个数字在某个百分比的时间内显示,random,functional-programming,bluegiga,Random,Functional Programming,Bluegiga,我正在寻找建立一个代码,使数字显示50%的时间,35%的时间和15%的时间。我是BGscript的新手,但我没有太多的运气使它可靠或工作。即使您没有使用其他语言编写任何BGscript。那太好了 我在这里写了一篇博客文章和在BGScript中生成随机无符号int的示例代码: 本质上,它使用由模块序列号和/或ADCs LSB噪声播种的xorshift来生成伪随机数 # Perform a xorshift (https://en.wikipedia.org/wiki/Xorshift) to ge

我正在寻找建立一个代码,使数字显示50%的时间,35%的时间和15%的时间。我是BGscript的新手,但我没有太多的运气使它可靠或工作。即使您没有使用其他语言编写任何BGscript。那太好了

我在这里写了一篇博客文章和在BGScript中生成随机无符号int的示例代码:

本质上,它使用由模块序列号和/或ADCs LSB噪声播种的xorshift来生成伪随机数

# Perform a xorshift (https://en.wikipedia.org/wiki/Xorshift) to generate a pseudo-random number
export procedure rand()
    t = x ^ (x << 11)
    x = y
    y = z 
    z = rand_number
    rand_number = rand_number ^ (rand_number >> 19) ^ t ^ (t >> 8)
end
#执行异或换档(https://en.wikipedia.org/wiki/Xorshift)生成一个伪随机数
出口程序()
t=x^(x>19)^t^(t>>8)
终止
并在此处初始化:

# Get local BT address
call system_address_get()(mac_addr(0:6))
...
tmp(15:1) = (mac_addr(0:1)/$10)+ 48 + ((mac_addr(0:1)/$10)/10*7)
tmp(16:1) = (mac_addr(0:1)&$f) + 48 + ((mac_addr(0:1)&$f )/10*7)
...
# Seed the random number generator using the last digits of the serial number 
seed = (tmp(15) << 8) + tmp(16)
call initialize_rand(seed)

# For some extra randomness, can seed the rand generator using the ADC results  
from internal temperature
    call hardware_adc_read(14, 3, 0)
end

event hardware_adc_result(input, value)
    if input = 14 then
        # Use ambient temperature check to augment seed
        seed = seed * (value & $ff)
        call initialize_rand(seed)
    end if
end
#获取本地BT地址
调用系统地址获取()(mac地址(0:6))
...
tmp(15:1)=(mac地址(0:1)/$10)+48+((mac地址(0:1)/$10)/10*7)
tmp(16:1)=(mac地址(0:1)和$f)+48+((mac地址(0:1)和$f)/10*7)
...
#使用序列号的最后几位为随机数生成器设定种子

seed=(tmp(15)正好是那些百分比,或者仅仅是平均值?这3个数字的列表是否总是可以被一个允许这些精确百分比的数字整除,如果不是,创建一个1到100的sudo随机数,如果>50,显示数字1,<35,显示数字2,否则显示数字3。生成一个0(包括)之间的随机整数和100(不含)从均匀分布。如果小于50,则返回备选方案a;否则如果小于85,则返回备选方案B;否则返回备选方案C。我在谷歌上搜索了随机数生成器,但BGScript中似乎没有,但此链接可能会有所帮助。是的,BGScript中没有def随机数生成器。我想获得clo尽可能多地考虑数字。基本上,我有一个装置,如果你按下按钮,它会振动、闪烁led或发出哔哔声。如果按下按钮,我想让振动在50%的时间里持续,超过35%的led在按下时继续,15%的哔哔声在按下时停止。看起来很简单,但我的脑袋在别处Y
call rand()
if rand_number <= PROBABILITY1 then
    # Show number 1
end if
if rand_number > PROBABILITY1 and rand_number <= PROBABILITY2 then
    # Show number 2
end if
if rand_number > PROBABILITY2 then
    # Show number 3
end if