Spawning items with different probabilities?

Hi guys,

As the title show, I want to spawn different items but not just randomly. For instance in my case I have bunch of items that the player need to collect (runner game) but some are more precious than others so I do not want to spawn them with same probability. So I want to spawn cheaper items more often than items that are more precious and should be spawn much less often.

I have to admit that my math skill are very rusty:)

Thanks for any suggestions.

Mo

www.j-strahan.com
go to freebies download js.lua
it has code to weight the results to be more lower or higher number

Thanks a lot! I will Check it out. Mo

look under downloads and activate.lua its an updated version

mRandom = math.random(1,10); if(mRandom == 1 or mRandom == 2)then        --do something end  

Probability: 2/10=1/5

@iStrahan: Thanks. The gameLog alone deserve a donation! Cool stuff!

@LavitzBr: Thanks a lot. That’s what I am basically doing. I just wanted a more “algorithmic” way of doing that :slight_smile:

I also found this on Stackoverflow:


Assuming that you have a function p(n) that gives you the desired probability for a random number:

r = rand() // a random number between 0 and 1
for i in A to B do
if r < p(i)
return i
r = r - p(i)
done

http://stackoverflow.com/questions/13635448/generate-random-numbers-within-a-range-with-different-probabilities


A and B are the range of the random number you want (ie: A = 1, B =10)  I guess p(n) is an array where you need to fill up with each category probability like:

p(1) = 0.5  – 50% chance for spawning a red ball

p(2) = 0.25 – 25% “”" yellow ball

p(3) = 0.05 – 5% “” green ball 

p(4P = 0.02 – 2% “” blue ball

At least that’s the way I read it.

I guess in Lua, we could do something like this (NOT TESTED):

[lua]

p = {0.5,0.25,0.05,0.02}

A= 1

B =10

local function chance (A,B)

  r = math.random()

  for i = A,B do

  if r < p(i) then

  return i

  else

  r = r - p(i)

  end

end

[/lua]

Please let me know if you see any errors!

Thanks a lot again guys. I think these time of random generation makes game more fun since you control how often an activity (enemies, powerups…) is generated.

Mo

thx Mo
appLig is old I have a newer version called blackBox that’s a lot better more options but not ready for download. if you want I’ll send you a copy. you may need to make a few change to make it work with graphics2.0 I plan to convert it but trying to finish what I’m working on right now

www.j-strahan.com
go to freebies download js.lua
it has code to weight the results to be more lower or higher number

Thanks a lot! I will Check it out. Mo

look under downloads and activate.lua its an updated version

mRandom = math.random(1,10); if(mRandom == 1 or mRandom == 2)then &nbsp; &nbsp; &nbsp; &nbsp;--do something end &nbsp;

Probability: 2/10=1/5

@iStrahan: Thanks. The gameLog alone deserve a donation! Cool stuff!

@LavitzBr: Thanks a lot. That’s what I am basically doing. I just wanted a more “algorithmic” way of doing that :slight_smile:

I also found this on Stackoverflow:


Assuming that you have a function p(n) that gives you the desired probability for a random number:

r = rand() // a random number between 0 and 1
for i in A to B do
if r < p(i)
return i
r = r - p(i)
done

http://stackoverflow.com/questions/13635448/generate-random-numbers-within-a-range-with-different-probabilities


A and B are the range of the random number you want (ie: A = 1, B =10)  I guess p(n) is an array where you need to fill up with each category probability like:

p(1) = 0.5  – 50% chance for spawning a red ball

p(2) = 0.25 – 25% “”" yellow ball

p(3) = 0.05 – 5% “” green ball 

p(4P = 0.02 – 2% “” blue ball

At least that’s the way I read it.

I guess in Lua, we could do something like this (NOT TESTED):

[lua]

p = {0.5,0.25,0.05,0.02}

A= 1

B =10

local function chance (A,B)

  r = math.random()

  for i = A,B do

  if r < p(i) then

  return i

  else

  r = r - p(i)

  end

end

[/lua]

Please let me know if you see any errors!

Thanks a lot again guys. I think these time of random generation makes game more fun since you control how often an activity (enemies, powerups…) is generated.

Mo

thx Mo
appLig is old I have a newer version called blackBox that’s a lot better more options but not ready for download. if you want I’ll send you a copy. you may need to make a few change to make it work with graphics2.0 I plan to convert it but trying to finish what I’m working on right now