简体   繁体   English

MYSQL / PHP-具有%的随机行

[英]MYSQL/PHP - Random row with %

I'm trying to make a random encounter with a monster based on %. 我正在尝试根据%与怪物进行随机接触。

I got in my MySQL database a column called "monster_chancespawn" : 我在MySQL数据库中找到了一个名为“ monster_chancespawn”的列:

  • Monster 1 got 50% 怪物1获得50%
  • Monster 2 got 30% 怪物2获得30%
  • Monster 3 got 20% 怪物3获得20%

What I want to do is to do a random MySQL SELECT in my "monster" DATABASE based on these %. 我想做的就是基于这些%在我的“ monster”数据库中进行一个随机的MySQL SELECT

I already got this 我已经知道了

    $monsterspawn=$db->prepare('SELECT * FROM monster');
    $monsterspawn->execute();
    $monsterspawn->CloseCursor();


    foreach ($monsterspawn as $infospawn)
      { 
      echo . $infospawn["monster_chanceloot"] . ;
      }

With this, it normally echo "50 30 20", but nothing really useful. 这样,它通常会回显“ 50 30 20”,但没有什么真正有用的。 What I want to do is : 我想做的是:

  • Getting each monster's chances of loot. 获得每个怪物的掠夺机会。
  • Maybe doing something like "If rand is between 0 and 50, spawn Monster 1. If it is between 50 and 80, spawn Monster 3. If it is between 80 and 100, spawn Monster 3. BUT I want this to be flexible, and to change when I change the monster_chanceloot value 可能会做类似“如果兰德介于0和50之间,则生成Monster1。如果介于50和80之间,则生成Monster3。如果介于80和100之间,则生成Monster3。但是我希望它具有灵活性,并且当我更改monster_chanceloot值时更改

I could do something like this I guess 我猜我可以做这样的事情

$randomspawn = rand(0, 100);
if ($randomspawn >= 0 && $randomspawn <= $infospawn["monster_chanceloot"])
{ $monstername = "Monster1" }

   elseif ($randomspawn >= $infospawn["monster_chanceloot"] && $randomspawn <= $infospawn["monster_chanceloot"](Monster 2's one))
    { $monstername = "Monster2" }

And same thing for Monster 3. The main problem would be that is does not let me change the number of monsters available, and the other problem is that I don't know how to get the monster_chanceloot of each monster outside of the foreach . 对怪物3来说也是一样。主要的问题是不允许我更改可用怪物的数量,另一个问题是我不知道如何在foreach之外获取每个怪物的monster_chanceloot。 And I don't want to make 30 MYSQL connection, so avoid using it each time. 而且我不想建立30个MYSQL连接,因此避免每次都使用它。

Thanks ! 谢谢 !

EDIT : Here is a possibility given by "German Drulyk" : 编辑:这是“德国德鲁利克”给出的一种可能性:

I got Monster1 , Monster2 , Monster3 . 我得到了Monster1Monster2Monster3 I duplicate Monster1 2times in my database and Monster2 1time. 我在数据库中复制了Monster1 2次,并复制了Monster2 1time。

I have now 3 Monster1 , 2 Monster2 and 1 Monster3 . 我现在有3个Monster1,2Monster2和1个Monster3 So : 50% of chance to pick a Monster1, 33,3 for Monster 2 and 16,6 for Monster 3. 所以:有50%的机会选择Monster1,为Monster 2选择33,3,为Monster 3选择16,6。

To get more accurate results, you can copy them to 100 to have 1 monster = 1%. 为了获得更准确的结果,您可以将它们复制到100以使1个怪物= 1%。 To get a monster, simply do : 要获得怪物,只需执行以下操作:

    $query=$db->prepare('SELECT * FROM MONSTERDDB ORDER BY RAND() LIMIT 1');
    $query->execute();

IF you will never exceed 100 monsters in a zone then you need a table with 3 columns; 如果您在一个区域内永远不会超过100个怪物,那么您需要一张3列的桌子; zone_id , chance , monster_id zone_idchancemonster_id

Per zone, insert 100 records and set the chance to 1 through 100 inclusively. 在每个区域中,插入100条记录,并将机会设置为1到100。

Per record, add a monster_id respective to it's common-ness; 每条记录,在其公共性上分别添加一个monster_id; all 100 records per zone should have a monster_id 每个区域的所有100条记录应具有monster_id

For example, zone1 needs 50 records with monster1, 30 records for monster2, and 20 records for monster3 例如,zone1需要50条带有Monster1的记录,30条Monster2的记录和20条Monster3的记录。

In PHP, you can write your query like so 在PHP中,您可以像这样编写查询

$sql = "select
          monster_id
        from
          table_name
        where
          zone_id = $zoneId and
          chance = ".rand(1, 100);

Additionally, I have not tried this but I guess native MySQL can achieve the same effect : 另外,我还没有尝试过,但是我想原生MySQL可以达到同样的效果

$sql = "select
          monster_id
        from
          table_name
        where
          zone_id = $zoneId and
          chance = FLOOR( 1 + RAND() * 100 )";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM