Skip to content

Instantly share code, notes, and snippets.

@FantasyVR
Created November 8, 2021 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FantasyVR/523c94f90c1ef955149b023d2c1cd9b0 to your computer and use it in GitHub Desktop.
Save FantasyVR/523c94f90c1ef955149b023d2c1cd9b0 to your computer and use it in GitHub Desktop.
"""
Loop:
Press start button to start random selection
Press end to show the selected ID
"""
import taichi as ti
ti.init(arch=ti.cpu,cpu_max_num_threads=1)
n = 100
userId = ti.field(ti.i32, shape=n) # userId is an array of 0s
@ti.kernel
def generate_luck_id() -> ti.i32:
numid = ti.cast((n * ti.random()), ti.i32) # random number in [0,n]
return numid
@ti.kernel
def checked_id(numid: ti.i32)->ti.i32:
is_used = 0 # test if a id is used before
if 0 <= numid < n and userId[numid] == 1:
is_used = 1
return is_used
@ti.kernel
def remove(numid: ti.i32):
if 0 <= numid < n:
userId[numid] = 1
gui = ti.GUI("Luck person", res=(600,600))
start = gui.button('start')
end = gui.button('end')
pause = True
numid = -1
while gui.running:
for e in gui.get_events(gui.PRESS):
if e.key == gui.ESCAPE:
gui.running = False
elif e.key == start:
pause = False
elif e.key == end:
pause = True
if not pause:
numid = generate_luck_id()
count = 0
while checked_id(numid) and count < 10: # if id is used, re-generate id
numid = generate_luck_id()
count += 1
if pause:
remove(numid)
gui.text(f"{numid}", pos=(0.5,0.5),font_size=50, color=0x00FF00)
gui.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment