Problem with simple card shuffle program

Hello all, sorry if this is just me being dopey, but I have been playing around and made a simple card shuffle program, but I get nonsensical results like "2 of Jack" and "0 of Diamonds". I just wanted to check if it's just me? I can't see why a face would become a suit in this instance?
from random import random_ui64, seed
from time import monotonic

fn draw(suits: List[String], faces: List[String], numbereds: List[Int]) raises -> String:
seed(int(monotonic()))

var rand_suit_idx = int(random_ui64(0, len(suits)))
var rand_suit = suits[rand_suit_idx]

# decide whether to use a face card or a numbered card
if random_ui64(0, 2) == 0:
var rand_face_idx = int(random_ui64(0, len(faces)))
return faces[rand_face_idx] + " of " + rand_suit
else:
var rand_numbered_idx = int(random_ui64(0, len(numbereds)))
return str(numbereds[rand_numbered_idx]) + " of " + rand_suit

fn main() raises -> None:
var suits = List[String]("Clubs", "Spades", "Hearts", "Diamonds")
var faces = List[String]("Jack", "Queen", "King", "Ace")
var numbered = List[Int](2, 3, 4, 5,6 , 7, 8, 9, 10)

print(draw(suits, faces, numbered))
from random import random_ui64, seed
from time import monotonic

fn draw(suits: List[String], faces: List[String], numbereds: List[Int]) raises -> String:
seed(int(monotonic()))

var rand_suit_idx = int(random_ui64(0, len(suits)))
var rand_suit = suits[rand_suit_idx]

# decide whether to use a face card or a numbered card
if random_ui64(0, 2) == 0:
var rand_face_idx = int(random_ui64(0, len(faces)))
return faces[rand_face_idx] + " of " + rand_suit
else:
var rand_numbered_idx = int(random_ui64(0, len(numbereds)))
return str(numbereds[rand_numbered_idx]) + " of " + rand_suit

fn main() raises -> None:
var suits = List[String]("Clubs", "Spades", "Hearts", "Diamonds")
var faces = List[String]("Jack", "Queen", "King", "Ace")
var numbered = List[Int](2, 3, 4, 5,6 , 7, 8, 9, 10)

print(draw(suits, faces, numbered))
Many thanks!
3 Replies
Ryulord
Ryulord6d ago
it's not clear from the docs but random_ui64 returns a value in the provided range inclusive on both ends so you have some out of bounds indexing happening
Zbornak
ZbornakOP6d ago
Ah thank-you! 🤦‍♂️
ModularBot
ModularBot6d ago
Congrats @Zbornak, you just advanced to level 15!

Did you find this page helpful?