Jump to content

If you have 0.76% chance of getting an item, how will you implement it?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
mr_skyflakes21

mr_skyflakes21

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
I'm just curious about something. I always find this at games.

For example you have a monster called X.
X's loots are item1, item2, item3.

item1's probability of being dropped by X is let's say, 16%.
item2 is 0.76%.
item3 is 25%.

How will you code it?

#2
mr_skyflakes21

mr_skyflakes21

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
I just found the solution:

1. randomize a value,
2. if the value is LESS THAN OR EQUAL to the percentage of the item, it will be dropped.

thanks for making my mind curious from time to time. :D

#3
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts

mr_skyflakes21 said:

I just found the solution:

1. randomize a value,
2. if the value is LESS THAN OR EQUAL to the percentage of the item, it will be dropped.

thanks for making my mind curious from time to time. :D

Yeah. But don't make it randomize if the value is generated by user.

If user generated number is less than or equal to the percentage, it drops. You can do it other way around also.

#4
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
generate a random number between zero and one. if the probability is above (or "above or equal"? not sure, but shouldn't matter for sufficietly many digits in the random number.) the random number, the event occurs.

edit: accidentally wrote below instead of above.

Edited by anotheruser, 24 July 2010 - 02:10 AM.


#5
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts

mr_skyflakes21 said:

I just found the solution:

1. randomize a value,
2. if the value is LESS THAN OR EQUAL to the percentage of the item, it will be dropped.

thanks for making my mind curious from time to time. :D
Watch how you code that though.
In your example.
Item 1 16%
Item 2 0.76%
Item 3 25%

If you're random number came up .001 (0.1%) then it's less than your cutoff value for Items 1,2 And 3.
Now that's fine if you wish to give all 3 Items, but you may want to add some checking that they get only the lowest % item.

#6
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
If it's dead, why doesn't it drop all items?
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Because it only carries 1 item. And it has x% chance of carrying that item.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users