I have the following bit of cython code:
cdef int[:] random_binary_string
random_binary_string = np.random.choice(a=np.array([0, 1]), size=num_bits)
The annotator highlights the second line, indicating to me that I should replace it with something in pure c. My question is about what the right way to do this is.
Solutions I found:
I can do something like the following:
from libc.stdlib cimport rand
convert_to_binary_representation(rand())
Here convert_to_binary_representation is a function I would write that would take the 32 bit integer and convert it to binary form as an array of ints valued in 0 and 1. I'd then glue these together to get the string of the desired size. While that works, I suspect it's possibly not the right answer given that I'm trying to make this code as fast as possible.
I also found this question: Canonical way to generate random numbers in Cython Based on the answer there, my guess is that the right thing to do is to wrap some tool from the C++ library . Perhaps this one: https://en.cppreference.com/w/cpp/numeric/random/independent_bits_engine ? I've gotten kind of lost on how to do this -- hopefully learning more c++ will help, but until then...
Question: What is the right way to replace that numpy call?
Further information on how I am using random_binary_string:
- This is for a Monte Carlo calculation for a scientific computing project, I don't need cryptographic security.
- Each bit represents a choice of whether or not to include a certain element in a set.
Aucun commentaire:
Enregistrer un commentaire