How to randomly generate two integer matrices $A$ and $B$, so that entries of 3 matrices $A$, $B$, and $AB$ are within certain range?

65 Views Asked by At

I ran into this question when writing a program. I need to generate two matrices, and calculate their product.

However, I must ensure all entries are within 8-bit signed integer range, i.e. $[-128, 128)$. Is there a way to algorithmically achieve this?

Furthermore, what if I need every intermediate result during calculation is also within such range?

Besides, I think the algorithm needs not to be deterministic, but with high probability is enough. For example, is there a way to randomly generate the initial $A$ and $B$, so that the entries of product $AB$ are highly likely to within $[-128, 127)$? If so, I can run the program several times to be lucky.


Update

The dimensions of $A$ and $B$ are inputs of the program, which are typically 1K ~ 5K.