How can I build my own randi function from scratch?

조회 수: 1 (최근 30일)
erica Emmanuel
erica Emmanuel 2021년 4월 17일
댓글: Walter Roberson 2021년 4월 17일
I want to create a randi function from scratch on Matlab, can anyone please help me do this?
  댓글 수: 3
David Hill
David Hill 2021년 4월 17일
Use sponge function of SHA-3 hash to generate bit-stream to use in generating random integers of the desired range.
Walter Roberson
Walter Roberson 2021년 4월 17일
Interesting, but SHA3 produced full numbers of bits, so it produces values 0 to 2^n-1. To produce random integers in the range A to B fairly, 2^n (number of values) would have to be divisible by (B-A+1). That can only happen if (B-A+1) is a power of 2.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2021년 4월 17일
What exactly is "from scratch"? Which tools are included, which one are excluded? E.g. where do you want to take your random bits from? Is using rand() okay or do you need your own pseudo random number generator?
What is the desired precision? If you use rand() as source of bits, remember that it replies values from the open interval (0,1) with 53 bit precision. So you get 2^53-2 different values (2^53 excluding 0 and 1). This is 2 * 3 * 5 * 53 * 157 * 1613 * 2731 * 8191. So if you want your input to get a random number from 1 to 7, it is not trivial to find an algorithm, which allows a perfect equi-distribution. If the small bias is a problem, you need a smart approach. This is easier, if you produce the random bits by your own.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by