Jump to content

Need to use mt_rand on a array lol


Monkuar

Recommended Posts

Okay, so my monsters table has a column called "droparray" for example: 7,2,5

 

7,2,5 are the id's of the items they could POSSIBLE get when they kill the monster :)  [intuitive eh?]

 

I need a way to just select a random number from those 3 numbers (or any amount) AND a way to let's say if 7 was a unique item, have it a less % chance.

 

Any ideas? :)

Edited by Monkuar
Link to comment
Share on other sites

Sounds like you need to normalize your data to me.

 

 

haha nice pun :P

 

But seriously, my items and stuff are already in a row.. I'm just going to use the WHERE IN clause in my query, so I'm just generating these random id's to select what items they will get after the monster has died. These id's will be used in a where in CLAUSE (in mysql) to grab the item info/etc and insert their "NORMALIZED" data to their inventory/etc

 

I'm going to have each mob have different loot, let's say Mob 2 will have a chance to get the items with the id of 2,3,5  so I just need to select a random of these to use in the where IN clause to grab all the data and insert it. (makes it very good for RNG based)

Edited by Monkuar
Link to comment
Share on other sites

$monsterdata['droparray'] = "5,2,1";
$item_id_loot = array($monsterdata['droparray']);
 
echo 'Drop Array: '.$item_id_loot[array_rand($item_id_loot)].'
 
This DOESN't work.... but this does:
 
 
 
$item_id_loot = array(5,2,1);
 
echo 'Drop Array: '.$item_id_loot[array_rand($item_id_loot)].'

 

 

Why? Wat the.. Is it because it's set to Varchar in column? (mysql)

 

Lawl, another php bug zz will report 

Edited by Monkuar
Link to comment
Share on other sites

 

$monsterdata['droparray'] = "5,2,1";
$item_id_loot = array($monsterdata['droparray']);
 
echo 'Drop Array: '.$item_id_loot[array_rand($item_id_loot)].'
 
This DOESN't work.... but this does:
 
 
 
$item_id_loot = array(5,2,1);
 
echo 'Drop Array: '.$item_id_loot[array_rand($item_id_loot)].'

 

 

Why? Wat the.. Is it because it's set to Varchar in column? (mysql)

 

Lawl, another php bug zz will report 

 

 

It's not a bug...

 

 

$monsterdata['droparray'] = "5,2,1";
$item_id_loot = array($monsterdata['droparray']);
 
//this would result in Array([0]=>"5,2,1")
Link to comment
Share on other sites

No it doesn't, because it just reads it as text.  It spits out "5,2,1" instead of 1 rand of each.  "5,2,1" is the same as array(5,2,1) but php thinks if you put in a variable array($temparray), $temparray = "5,2,1" it think's it's 1 word, it's not.  

Link to comment
Share on other sites

No it doesn't, because it just reads it as text.  It spits out "5,2,1" instead of 1 rand of each.  "5,2,1" is the same as array(5,2,1) but php thinks if you put in a variable array($temparray), $temparray = "5,2,1" it think's it's 1 word, it's not.  

 

"5,2,1" is one string. And PHP reads it correctly. You are acting as if commas are some deliminator when you put a string in an array.

 

Like I said the array will be Array([0]=>"5,2,1"). Which is correct and not a bug.

Link to comment
Share on other sites

 

 

$monsterdata['droparray'] = "5,2,1";
$item_id_loot = array($monsterdata['droparray']);
 
echo 'Drop Array: '.$item_id_loot[array_rand($item_id_loot)].'
This DOESN't work.... but this does:

 

 

 

 

$item_id_loot = array(5,2,1);
 
echo 'Drop Array: '.$item_id_loot[array_rand($item_id_loot)].'

Why? Wat the.. Is it because it's set to Varchar in column? (mysql)

 

Lawl, another php bug zz will report

Revise the first code.

String literals are never parsed as arrays. Use explode() to get an array out of a CSV-list.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.