Ask Your Question
2

Can we find a more efficient method to generate random numbers in the range of 1 to n, where n is greater than 33000?

asked 2022-09-11 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-11-10 09:00:00 +0000

scrum gravatar image

Yes, there are more efficient methods to generate random numbers in the range of 1 to n where n is greater than 33000. One such method is the following:

  1. Choose a random number k from a uniform distribution of integers in the range of 1 to n^2.
  2. If k is greater than n, then set k = k % n + 1.
  3. Return k.

This method takes advantage of the fact that a uniform distribution of integers in the range of 1 to n^2 is easy to generate using a standard random number generator, and that the probability of k being outside the range of 1 to n is very small. By reducing k modulo n and adding 1, we ensure that the resulting number is in the range of 1 to n with equal probability.

This method has a time complexity of O(1) and is therefore more efficient than other methods that require multiple random number generations or arithmetic operations.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-09-11 11:00:00 +0000

Seen: 15 times

Last updated: Nov 10 '21