How to generate a random number of n bits length?
Mostra commenti meno recenti
Let's say a function takes input n as the bit length and outputs a random number. For example, for n = 4, the output range is [0 - 15] which also includes the range for n = 3 i.e. [0 - 7]. The function should generate the number in the range [8 - 15] and not in the range [0 - 15]. How to do this and how to generalize it for any n?
3 Commenti
KALYAN ACHARJYA
il 6 Set 2018
Modificato: KALYAN ACHARJYA
il 6 Set 2018
The function should generate the number in the range [8 - 15] and not in the range [0 - 15]. How to do this and how to generalize it for any n?
Can you clarify this statement? if n=3 then ??
What I am getting you want three-bit representation(when n=3) in the range [8 - 15]??
Mohsin Shah
il 6 Set 2018
KALYAN ACHARJYA
il 6 Set 2018
Modificato: KALYAN ACHARJYA
il 6 Set 2018
@Mohsin Check my answer, as per your requirement? If any modification required, let me know.
Risposta accettata
Più risposte (1)
KALYAN ACHARJYA
il 6 Set 2018
Modificato: KALYAN ACHARJYA
il 6 Set 2018
function bit_result=rand_bits(n)
result=randi([2^(n-1), 2^n]);
bit_result=de2bi(result,n)
end
3 Commenti
Mohsin Shah
il 6 Set 2018
KALYAN ACHARJYA
il 6 Set 2018
Modificato: KALYAN ACHARJYA
il 6 Set 2018
@Mohsin I have edited the answer, can you verify it?
Mohsin Shah
il 6 Set 2018
Categorie
Scopri di più su Random Number Generation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!