What is the most efficient and portable way to generate a random random in [0,1]
in Cython? One approach is to use INT_MAX
and rand()
from the C library:
from libc.stdlib cimport rand
cdef extern from "limits.h":
int INT_MAX
cdef float randnum = rand() / float(INT_MAX)
Is it OK to use INT_MAX
in this way? I noticed that it's quite different from the constant you get from Python's max int:
import sys
print INT_MAX
print sys.maxint
yields:
2147483647 (C max int)
9223372036854775807 (python max int)
Which is the right "normalization" number for rand()
? EDIT additionally, how can the random seed be set (e.g. seeded based on current time) if one uses the C approach of calling rand()
from libc?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…