January 26, 2021, 05:49:34 AM

### Author Topic: [bb] Random number with weighting by Warpy [ 1+ years ago ]  (Read 479 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Random number with weighting by Warpy [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Random number with weighting
Author : Warpy
Posted : 1+ years ago

Description : Suppose you want to make a random choice between a given set of options, but you want to make some options more likely than others.

I've included two methods here - a 'lazy' one that is fast for small numbers of options (<10) and a binary search method that is fast for large numbers of options.

Code :
Code: BlitzBasic
1. Function picksomething(probabilities#[])
2.    p#=rnd(0,1)
3.    t#=0
4.    i=0
5.    while t<=p
6.       t:+probabilities[i]
7.       i:+1
8.    wend
9.    return i - 1
10. End Function
11.
12.
13. Function picksomethingbinary(probabilities#[],sumprobabilities#[])
14.         l=Len(probabilities)
15.
16.         p#=Rnd(0,1)
17.         i=l/2
18.         move=i
19.         While 1
20.                 move:/2
21.                 If move=0 Then move=1
22.                 s#=sumprobabilities[i]
23.                 If s<p
24.                         i:+move
25.                 ElseIf s-probabilities[i]>p
26.                         i:-move
27.                 Else
28.                         Return i
29.                 EndIf
30.         Wend
31.         Return i - 1
32. End Function
33.
34.
35. 'EXAMPLE
36.
37. Local probabilities#[5]
38. probabilities=[.1,.1,.1,.2,.5]
39.
40. Local sumprobabilities#[5]
41. t#=0
42. For i=0 To 4
43.         t:+probabilities[i]
44.         sumprobabilities[i]=t
45. Next
46.
47. ms=MilliSecs()
48. For i=1 To 100000
49.         number=picksomethingbinary(probabilities,sumprobabilities)
50. Next
51. diff=MilliSecs()-ms
52. Print "binary search method took "+String(diff)+"ms"
53.
54. ms=MilliSecs()
55. For i=1 To 100000
56.         number=picksomething(probabilities)
57. Next
58. diff=MilliSecs()-ms
59. Print "lazy method took "+String(diff)+"ms"