Generate random number outside of range in python

后端 未结 5 1233
盖世英雄少女心
盖世英雄少女心 2021-02-18 22:52

I\'m currently working on a pygame game and I need to place objects randomly on the screen, except they cannot be within a designated rectangle. Is there an easy way to do this

5条回答
  •  后悔当初
    2021-02-18 23:20

    1. Partition the box into a set of sub-boxes.
    2. Among the valid sub-boxes, choose which one to place your point in with probability proportional to their areas
    3. Pick a random point uniformly at random from within the chosen sub-box.

    This will generate samples from the uniform probability distribution on the valid region, based on the chain rule of conditional probability.

提交回复
热议问题