Jump to content

Generating a random number by your set values


slyte33

Recommended Posts

Basically I'm making crimes in my game, you enter how many turns you want to use then foreach turn do something:

 

$times = $_POST[turns];
for($i=1; $i<=$times; $i++)
{
echo "#$i: ";

$query=$db->execute("select * from allpitems");
while($item = $query->fetchrow())
{

$rand = mt_rand(1, $item[itemfind]);

if ($rand == $item[itemfind])
{
echo "You found $item[name]<br><br>";
}else if ($rand != $item[itemfind]){
echo "You did not find anything<br><br>";
}
}

 

 

Since this is a while loop inside a for loop, each $i is going to loop X times(X being the number of rows in the table all_items). I know a way to stop it and fix this, by going about a different way:

 

What I want to do is make it generate a set of numbers, to be exact these numbers:

 

1, 2, 4, 8, 33, 83, 122, 125

 

so instead of using:

 

mt_rand(1, 125);

I only want it to generate the numbers I want, is this possible?

Thanks :)

 

http://en.wikipedia.org/wiki/Indent_style

 

Pick one and use it. Unindented/unformatted code is difficult to read for humans.

 

What I want to do is make it generate a set of numbers, to be exact these numbers:

 

1, 2, 4, 8, 33, 83, 122, 125

 

How is it random when you've already decided that you want exactly those numbers? Maybe I'm missing something...

http://en.wikipedia.org/wiki/Indent_style

 

Pick one and use it. Unindented/unformatted code is difficult to read for humans.

 

What I want to do is make it generate a set of numbers, to be exact these numbers:

 

1, 2, 4, 8, 33, 83, 122, 125

 

How is it random when you've already decided that you want exactly those numbers? Maybe I'm missing something...

 

I want those numbers exactly because i'm making crimes, some crimes are steal food, steal clothes, steal jewelry.

Each item you can own is in the all_items table with a set type(enum) and a set item_id

 

When stealing clothes, I want those item_id's only to be picked in a random sequence, so therefore you'd only able to steal clothes and not every type of item in the database :)

 

This is

I think slyte33 means something like this:

 

<?php
   $arr=array(1, 2, 4, 8, 33, 83, 122, 125);
   echo $arr[rand(0,count($arr))];
?>

 

Same concept as a bag in Java.

 

Thank you :)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.