Generate random array of 0 and 1 with a specific ratio

后端 未结 4 2091
孤城傲影
孤城傲影 2021-02-19 06:09

I want to generate a random array of size N which only contains 0 and 1, I want my array to have some ratio between 0 and 1. For example, 90% of the array be 1 and the remainin

4条回答
  •  有刺的猬
    2021-02-19 06:38

    Without using numpy, you could do as follows:

    import random
    percent = 90
    
    nums = percent * [1] + (100 - percent) * [0]
    random.shuffle(nums)
    

提交回复
热议问题