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:
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
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.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.
No worries
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. I got these number 7 times in a row. The odds are ridiculous really. I might go to Vegas if it's legit random.
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()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
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.
Hello, here is an additional way:
Note: Documentation: random for more functions
(
rand
and randn
for different distributions (normal/uniform), for example)