Help I have skill issues

I'm new to mojo and am still having trouble navigating the syntax. Currently, I'm trying to build a simple random number generator using the random module. The following is the function that's giving me problems. It says that I cannot implicitly convert 'None' value to SIMD[uint8, 1]. I think the real issue is that I'm unclear as to how to instantiate a pointer for the random variable. Originally, I put the unsafe pointer in the number variable but moved it to it's own variable in hopes of resolving the issue. I'm sure that I'm making a stupid mistake, but I am really hoping to learn how to write in this language as it would be a great addition to the field. Here is my function:
import random

fn exercise() -> UInt8:
var pointer = UnsafePointer[SIMD[DType.uint8, 1]].alloc(1)
var number = random.randint(pointer, 1, 0, 42)
return number
import random

fn exercise() -> UInt8:
var pointer = UnsafePointer[SIMD[DType.uint8, 1]].alloc(1)
var number = random.randint(pointer, 1, 0, 42)
return number
I properly used a main function. The error appears in 'return number'. If you guys could help me understand what I'm doing wrong and how to correct it, I would greatly appreciate it.
9 Replies
Nick!
Nick!3w ago
Per the docs for randint, it doesn't return anything. (There is no -> in its signature.) So number is None. The random number is written to the first argument—the pointer. Also, SIMD[Dtype.uint8, 1] is the same type as UInt8. This is discussed here. Also, you don't need to allocate your integer on the heap (alloc) in order to obtain an unsafe pointer to an integer. You can use address_of to get a pointer to a stack-allocated integer. Hopefully that's a useful starting point. Let me know if you need more info.
christian_drake
christian_drakeOP3w ago
I'm looking over everything now. I'd read through some of it, but I've really only messed with python and rust. manual memory management is difficult for me to wrap my head around. I'll read the docs and tinker around with it for a while and post more focused questions as I try to learn. Thank you.
Nick!
Nick!3w ago
No worries
christian_drake
christian_drakeOP3w ago
So looking over the random module documentation, what does it mean that randint "fills memory" instead of returning a value? What is the usage of filling memory in mojo? Also, I'm noticing that random_si64(0, 42) is consistantly coming up with the same numbers in order. I've compared multiple runs now using a 10 count to verify. This is the order that I've been getting.
5
32
19
22
9
2
29
29
40
5
32
19
22
9
2
29
29
40
I got these number 7 times in a row. The odds are ridiculous really. I might go to Vegas if it's legit random.
Nick!
Nick!3w ago
It fills the chunk of memory that the pointer points to check the description of each argument in the docs ptr: The pointer to the memory area to fill. size: The number of elements to fill. If your pointer points to a single integer, then size must be 1, otherwise you'll be writing to invalid memory locations. randint is designed to fill an entire buffer with random numbers if ptr points to a buffer, then size would be the number of random numbers that you want to put into the buffer Check out seed()
christian_drake
christian_drakeOP3w ago
yeah, for the random_si64, I used a while x < 10 and just had the random run that many times. My mind is still having trouble understanding how to use the randint pointer, but I'll keep cracking at it thanks, I'll look through the seed documentation
Nick!
Nick!3w ago
fn main():
random.seed()
for _ in range(10):
var x: UInt8 = 0
random.randint(UnsafePointer.address_of(x), 1, 0, 42)
print(x)
fn main():
random.seed()
for _ in range(10):
var x: UInt8 = 0
random.randint(UnsafePointer.address_of(x), 1, 0, 42)
print(x)
christian_drake
christian_drakeOP3w ago
Thank you so much. I'd gotten to the point that I was trying 'address_of( 0X01 )' lol Now I have something to work from.
rd4com
rd4com3w ago
Hello, here is an additional way:
from random import random_ui64, seed
def main():
seed()
for _ in range(10):
var x: UInt8 = random_ui64(0, 255).cast[DType.uint8]()
print(x)
from random import random_ui64, seed
def main():
seed()
for _ in range(10):
var x: UInt8 = random_ui64(0, 255).cast[DType.uint8]()
print(x)
Note: Documentation: random for more functions (rand and randn for different distributions (normal/uniform), for example)
Want results from more Discord servers?
Add your server