How to generate random uint64 values
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Tammy Kolda
il 12 Giu 2021
Commentato: Tammy Kolda
il 15 Giu 2021
MATLAB supports uint64. I need to generate random uint64 values, e.g., greater than (2^52-1) which is the largest that can be represented as a double with its 52-bit mantissa. (Or is it 53 bit?) In any case, I want to be able to go all the way up to intmax('uint64') if needed. But there does not seem to be any support for this. The following command produces an error, but this is essentially what I'm looking for...
r = randi(intmax('uint64'),100,1,'uint64')
Presumably I could generate two 'uint32' arrays and convert them to uint64, but I'm wondering if there are any builtin ideas.
I'm currently using MATLAB R2020b.
Thanks in advance for any ideas.
Risposta accettata
Più risposte (2)
Bruno Luong
il 12 Giu 2021
I can't see why you are reluctant to generate 2 x 4 bytes
r = typecast(randi(intmax('uint32'),2*100,1,'uint32'),'uint64')
Bruno Luong
il 14 Giu 2021
Modificato: Bruno Luong
il 14 Giu 2021
maxval = int64(2^60);
n = 100;
twop32 = 2^32;
q = double(maxval/twop32);
hi = floor(q*rand(1,n));
himax = floor(q);
lomax = twop32 + zeros(1,n);
lomax(hi == himax) = mod(maxval,twop32);
lo = ceil(lomax.*rand(1,n));
r = uint64(lo) + uint64(twop32)*uint64(hi);
disp(r)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!