Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Influence appearance chance

    Hey there, using this mysql string:

    Code:
    "SELECT name FROM monsters ORDER BY RAND() LIMIT 1"
    I retrieve a random monster encounter from my database. I have a question though, how would one influence the apperance rate of particular rows from my table? In practices this means that some monsters would appear more, some less.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    One easy way is to have duplicates of some monsters. The more duplicates, the more likely that monster is to come up randomly.alter

    But if you want finer control, you could do something like this:
    Code:
    mysql> select * from monsters;
    +----------+------+------+
    | monster  | low  | high |
    +----------+------+------+
    | monster1 |    0 |    9 |
    | monster2 |   10 |   29 |
    | monster3 |   30 |   59 |
    | monster4 |   60 |   99 |
    +----------+------+------+
    See it? monster 1 has a range of 10 numbers, monster 2 has a range of 20 numbers, etc.

    And then use a query such as this:
    Code:
    SELECT * 
    FROM ( SELECT FLOOR(100 * RAND()) AS rnum ) AS R, monsters AS M 
    WHERE R.rnum BETWEEN M.low AND M.high
    And get results such as these:
    Code:
    +------+----------+------+------+
    | rnum | monster  | low  | high |
    +------+----------+------+------+
    |   15 | monster2 |   10 |   29 |
    +------+----------+------+------+
    
    +------+----------+------+------+
    | rnum | monster  | low  | high |
    +------+----------+------+------+
    |    0 | monster1 |    0 |    9 |
    +------+----------+------+------+
    
    +------+----------+------+------+
    | rnum | monster  | low  | high |
    +------+----------+------+------+
    |   59 | monster3 |   30 |   59 |
    +------+----------+------+------+
    You can adjust the proportions as you want, of course.
    And you do *NOT* have to have a maximum number of 99, as I have here.

    Just make sure that:
    (a) All numbers from 0 to your maximum are used for some monster, and
    (b) The number you use in place of 100 in the expression FLOOR( 100 * RAND() ) is one more than your maximum.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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