Results 1 to 3 of 3

Thread: weighted random generation

  1. #1

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    weighted random generation

    I've thought on this one for a week or so with no avail, so maybe someone else can help me. Say you have 4 'items', 0-3, and you want the latter items to generated less often then the beginning ones. You could say:
    x = rnd * 100
    if x > 90 then itemId = 3
    else if x > 70 then itemId = 2
    else if x > 40 then itemId = 1
    else if x >= 0 then itemId = 0
    end if
    this produces a weighted random generation (10% for #3, 20 % for #2...etc). That's all fine and dandy, but say you want to adjust these as the game progresses. Say, at the beginning id 3 has a 0% chance to be called, but later it has a 20% chance. That requires a readjustment of all the numbers, or a fundamentally different way of having a weighting random generation. The latter part I feel is probably the solution to this problem, but I would like some insight.
    thanks,
    jmiller

  2. #2
    New Member
    Join Date
    Jul 2002
    Location
    Maryland
    Posts
    4
    The percentage for an item should be stored as a variable with each item.

    I see you reference an ItemID. Well I would suggest creating a TypeDef of items that includes its precent value.

    Then take this approach:

    Type ItemType
    Name as String
    LoPercent as Integer
    HiPercent as Integer
    End Type


    Dim Item(10) as ItemType


    Function RandomItem% ()
    x = rnd *100

    For i = 1 to num_Items

    Get Item(i)

    If Item(i).HiPercent > x and Item(i).LoPercent < x then
    itemId =x
    i = num_Items
    End if

    Next i

    RandomItem% = itemId
    End Function


    I've used some routines similar to this in game development. I don't know what you are doing, but that little code will give you a start on how to grab a random item based on percentages.
    Explorations RPG System
    www.explore-rpg.com

  3. #3

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    Exclamation

    In understand what your saying, but there is still a problem. You see, each item must have its own high and low range that no other item shares. For example,

    item(0).low = 0 and item(0).high = 10
    item(1).low = 10 and item(1).high = 20
    x = rnd * 20
    if x >= item(i).low and x < item(i).high then choose the item #.

    i did this with ten items, 0-9, and gave them each a range of 10 (x = rnd * 100). After looping 100,000 times and keeping track of how many times each was choosen, they each were chosen about 10% of the time (go figure...). The problem with having the items have overlapping ranges is that it's possible for a number x to be equal to several different items, and the choice of items being made solely upon the value of i.
    For instance say item 1 and item 7 share the range 10(low) and 20(high). If x is 15 then it could be 1 or 7, and whenever x = 10-20 it will always choose item # 1 because that comes first in the loop (i = 0 to 9).
    The problem with having ranges that don't overlap is that when you want to change the range of one, you have to change the range of the rest. It is this problem of which I am wondering if there is another way around.
    thanks again,
    jmiller

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width