Different probability for ranges of random numbers

前端 未结 2 1779
感情败类
感情败类 2021-01-03 05:30

I\'m looking for the best way of implementing random number generator, that will allow me to have control over probability from what range the generated number will be retur

2条回答
  •  轮回少年
    2021-01-03 06:30

    Hmm, working on your original I had a pretty simple algorithm to generate ranges in an array in the appropriate proportion, then randomly select a range and generate a random number within that range. No doubt it can be optimised if necessary, but it works for me.

    It looks like a lot of code, but 3/4 of it is comments, test data and function, the actual randomRange function is only 17 lines of code.

    
    
    
    
    
    Numbers 0 - 20
    Numbers 21 - 400

提交回复
热议问题