找回密码
 点一下
查看: 4910|回复: 16

随机数生成器

[复制链接]
发表于 2009-1-16 00:19:00 | 显示全部楼层 |阅读模式
总有人抱怨jass自带的随机生成函数不够好,这个也许会好一些,但代价是更慢。

请允许我引用一下
Inversive Congruential Generators
Sometimes the Parallel Hyperplanes phenomenon inherentin LCGs may cause adverse effects to certain simulation applications becausethe space between the hyperplanes will never be hit by any point of thegenerator, and the simulation result may be very sensitive to this kind ofregularities. Inversive Congruential Generators (ICG) are designed to overcome this difficulty. It is a variant of LCG:

where if and . Tocalculate , one can apply the reverse of Euclid's algorithm to find integer solutions for .Although the extra inversion step eliminates Parallel Hyperplanes(see Figure 3), it alsochanges the intrinsic structures and correlation behaviors of LCGs [5]. ICGs are promising candidates for parallelization, becauseunlike LCGs, ICGs do not have long-range autocorrelations problems.However, at current state of the art, ICGs are substantially (8X) slower than LCGs due to the inversion process. ICGs have the same period as their LCG counterparts, i.e. .

注:ICG(逆同余生成器)即下面的,LCG(线性同余生成器)即原jass的(猜的)。

LCG





ICG


[codes=jass]
function ICG_GetInvert takes integer c returns integer
    local integer s = 0
    local integer t = 1
    local integer n = c
    local integer p = udg_ICG_max
    local integer q = 0
   
    if c == 0 then
        set c = 1
    endif
    loop
        set q = p / n
        set s = s - q * t
        set p = p - q * n
        if p == 0 then
            return t
        endif
        set q = n / p
        set t = t - q * s
        set n = n - q * p
        if n == 0 then
            return s
        endif
    endloop
    return s
endfunction

function ICG_GetMult takes integer a, integer b returns integer
    local integer q = 0
    if a == 1 then
        return b
    elseif b <= udg_ICG_max / a then
        return a * b
    else
        set q = udg_ICG_max / a
        set b = a*ModuloInteger(b, q) - b/q*ModuloInteger(udg_ICG_max, a)
        if b < 0 then
            return b + udg_ICG_max
        else
            return b
        endif
    endif
    return b
endfunction
  
function ICG_GetAdd takes integer a, integer b returns integer
    if b <= udg_ICG_max - a then
        return a + b
    else
        return b - udg_ICG_max + a
    endif
    return b
endfunction

function ICG_Seed takes integer s returns nothing
    set udg_ICG_max = 2147483647
    set udg_ICG_a = 9102
    set udg_ICG_b = 2110599482
   
    if s == 0 then
        set udg_ICG_seed = GetRandomInt(1, udg_ICG_max)
    else
        set udg_ICG_seed = s
    endif
endfunction

function ICG_Random takes nothing returns integer
    set udg_ICG_seed = ICG_GetAdd(ICG_GetMult(ICG_GetInvert(udg_ICG_seed), udg_ICG_a), udg_ICG_b)
    return udg_ICG_seed
endfunction
[/codes]

评分

参与人数 2威望 +6 收起 理由
eff + 5
血戮魔动冰 + 1 优秀文章

查看全部评分

发表于 2009-1-16 11:01:23 | 显示全部楼层
哇塞
好多雪花
从图形看效果是更好了
回复

使用道具 举报

发表于 2009-1-19 14:16:29 | 显示全部楼层
哇,支持一下。
回复

使用道具 举报

发表于 2009-1-25 12:24:54 | 显示全部楼层
不是很明白。。说效果的话要怎么看呢,那些对比的雪花图
看上去都是联张图案的样子
回复

使用道具 举报

发表于 2009-1-25 13:00:16 | 显示全部楼层
就是随机数的分布问题,明显前面的一组很规律,而后面的则没有什么规律
回复

使用道具 举报

发表于 2009-1-25 13:28:25 | 显示全部楼层
感觉图案都是一致的
回复

使用道具 举报

发表于 2009-1-25 13:33:23 | 显示全部楼层
你觉得那两张图是一致的么………………
原来线性和雪花看上去是一样的………………
回复

使用道具 举报

发表于 2009-1-25 14:25:37 | 显示全部楼层
所以才看不得懂图示分布的意思
回复

使用道具 举报

发表于 2009-1-25 15:32:25 | 显示全部楼层
突然发现 actboy是16号00:19的时候做出来的~~~~~~

楼主太强大了~~~~~~~~我顶~~~~~~
回复

使用道具 举报

发表于 2009-1-25 16:10:59 | 显示全部楼层
ls的新人大叔你好~
16号有什么特殊意义呢?
回复

使用道具 举报

发表于 2009-1-25 22:11:10 | 显示全部楼层
此类学术型实用函数顶之~~~不过仅限内部参考。。
回复

使用道具 举报

发表于 2009-1-25 22:20:08 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

发表于 2009-1-28 12:59:20 | 显示全部楼层
我也好久没上了
回复

使用道具 举报

发表于 2009-2-2 12:03:12 | 显示全部楼层
正需要
回复

使用道具 举报

发表于 2009-2-3 08:57:50 | 显示全部楼层
真头疼啊,如果想获得A,B之间的随机数要怎么做?
回复

使用道具 举报

 楼主| 发表于 2009-3-3 14:27:20 | 显示全部楼层
在初始化时,先用call ICG_Seed(seed)进行初始化,ICG_Random()会得到一个0~udg_ICG_max之间的一个随机数




其实war3不是同线性同余的,原谅我的无知吧.....
回复

使用道具 举报

发表于 2009-3-3 16:13:42 | 显示全部楼层
…………其实…………随机数用什么都可以吧~~因为根据不同的运行次序情况,就算是1,2,3,4,5,6,…………的随机数序列,也会有很多种的真实情况~~
不过强大~顶~~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 点一下

本版积分规则

Archiver|移动端|小黑屋|地精研究院

GMT+8, 2024-5-5 21:28 , Processed in 0.176094 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表