随机数生成问题

import taichi as ti
ti.init(arch=ti.cpu)
@ti.func
def randint(a:ti.i32,b:ti.i32)->ti.i32:
    return ti.random(ti.i32)%(b-a+1)+a
@ti.kernel
def foo():
    print(randint(1,3))
    print(randint(2,7))
    print(randint(1,10))
foo()

我想随机产生【a, b】之间的整数,但运行多次结果却是一样的,这是为什么呢?微信图片_20211209220559

Hi @Ming_CHEN, 需要加入随机数种子:

import time
import taichi as ti
ti.init(random_seed=int(time.time()))
2 Likes

谢谢禹鹏老师!