Random number generator within a range of numbers excluding another range of numbers.

4 views (last 30 days)
I have a random number generator
peg = ceil(15*(rand()+1e-15))% pick random peg from the board
i want to exclude specific values from this generator. specifically 11, 8, and 7. how would i go about doing that.

Answers (1)

John D'Errico
John D'Errico on 7 May 2014
Edited: John D'Errico on 7 May 2014
Simple is to delete the elements that fall into the excluded set. Then resample for the deleted ones. This rejection sampling scheme is an unbiased solution and trivial to implement.
As simple is to sample from the set 1:12. You can use the same scheme if you wish, or use a tool like randi. (My guess is you are not allowed to use randi as this is homework.) Then use that as an index INTO the vector setdiff(1:15,[7 8 11]). Again, unbiased and trivial.

Categories

Find more on Card games in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!