0

Let's say you have a random algorithm which can choose between 2 elements however you want the 2nd element to have a chance of only 1% to be selected. What is the algorithmic logic behind it? Also any idea on how I could find more on this matter? I researched random algorithms with determined output but couldn't find answer to my question.

I looked into Cryptographically secure pseudorandom number generator however I think that for now, it is not necessary to overcomplicate things.

Iulian
  • 409
  • 3
  • 17
  • uh well a simple way to do it, though probably not truly random and probably not usable for crypto would be .. use the languages random function to generate a number between 0 and 99... if 0 then pick the second element else pick the first – mad.meesh Sep 20 '18 at 22:42
  • 2
    Related: [Generate a weighted random number](https://stackoverflow.com/questions/8435183/generate-a-weighted-random-number) – CRice Sep 20 '18 at 22:43

1 Answers1

1
int arr[2] = {1, 2};
return arr[(Math.floor(Math.random() * 100) == 0)? 1 : 0];
olegarch
  • 3,670
  • 1
  • 20
  • 19