-4

I am looking for a scrip that has a precentage chance of having a certain image. IE, 5% chance of 1_image spawning and 95% of image_2 spawning. Can anyone give me tips or coding examples for this?

  • Can you clarify what you mean, please? If you are talking about setting probablities you look at [getRand()](https://www.geeksforgeeks.org/generate-0-1-25-75-probability/#:~:text=Given%20a%20function%20rand50%20%28%29%20that%20returns%200,the%20number%20of%20calls%20to%20rand50%20%28%29%20method.) and this: https://stackoverflow.com/questions/21478905/how-to-select-random-pictures-in-java. – Alias Cartellano Sep 23 '21 at 21:15
  • 1
    Does this answer your question? [Generate random integers with probabilities](https://stackoverflow.com/questions/8877249/generate-random-integers-with-probabilities) – ATP Sep 23 '21 at 21:32
  • Sorry, I meant Rand() and Math.Rand(). – Alias Cartellano Sep 23 '21 at 21:47

1 Answers1

0
95/100 = 19/20
 5/100 =  1/20

simply do something like this:

var rand = Math.floor(Math.rand()*20); // generate random number between 0-19. total of 20 numbers 
if(Math.rand()==0) displayImage1(); the chances to get 0 in a random number between 0-19 is 1/20
else displayImage2(); //otherwise (the 19/20 chances)

Or like this:

 if(Math.rand()<0.05) displayImage1();
    else displayImage2(); 

See this similar question

ATP
  • 2,939
  • 4
  • 13
  • 34