/    Sign up×
Community /Pin to ProfileBookmark

[PHP] [BASIC] Need help with randoms!

I just noticed my post was in the wrong section.
Could anybody help me turning this code into having chances? I seem to have a few problems.

Here is the code:

[code=php]<?php
$items = array(
array(‘weapon’ => ‘AK-47’, ‘name’ => ‘Elite Build1’, ‘look’ => 1, ‘color’ => ‘#96281B’, ‘image’ => ‘assets/img/ak/1.png’),
array(‘weapon’ => ‘AK-47’, ‘name’ => ‘Elite Build2’, ‘look’ => 1, ‘color’ => ‘#D24D57’, ‘image’ => ‘assets/img/ak/2.png’),
array(‘weapon’ => ‘AK-47’, ‘name’ => ‘Elite Build3’, ‘look’ => 1, ‘color’ => ‘#DB0A5B’, ‘image’ => ‘assets/img/ak/3.png’),
array(‘weapon’ => ‘AK-47’, ‘name’ => ‘Elite Build4’, ‘look’ => 1, ‘color’ => ‘#663399’, ‘image’ => ‘assets/img/ak/4.png’),
array(‘weapon’ => ‘AK-47’, ‘name’ => ‘Elite Build5’, ‘look’ => 1, ‘color’ => ‘#674172’, ‘image’ => ‘assets/img/ak/5.png’),
array(‘weapon’ => ‘AK-47’, ‘name’ => ‘Elite Build6’, ‘look’ => 1, ‘color’ => ‘#9A12B3’, ‘image’ => ‘assets/img/ak/6.png’),
array(‘weapon’ => ‘AK-47’, ‘name’ => ‘Elite Build7’, ‘look’ => 1, ‘color’ => ‘#446CB3’, ‘image’ => ‘assets/img/ak/7.png’),
array(‘weapon’ => ‘AK-47’, ‘name’ => ‘Elite Build8’, ‘look’ => 1, ‘color’ => ‘#2C3E50’, ‘image’ => ‘assets/img/ak/8.png’),
array(‘weapon’ => ‘AK-47’, ‘name’ => ‘Elite Build9’, ‘look’ => 1, ‘color’ => ‘#3A539B’, ‘image’ => ‘assets/img/ak/9.png’),
array(‘weapon’ => ‘AK-47’, ‘name’ => ‘Elite Build10’, ‘look’ => 1, ‘color’ => ‘#26A65B’, ‘image’ => ‘assets/img/ak/10.png’),
array(‘weapon’ => ‘AK-47’, ‘name’ => ‘Elite Build11’, ‘look’ => 1, ‘color’ => ‘#2ECC71’, ‘image’ => ‘assets/img/ak/11.png’),
array(‘weapon’ => ‘AK-47’, ‘name’ => ‘Elite Build12’, ‘look’ => 1, ‘color’ => ‘#E87E04’, ‘image’ => ‘assets/img/ak/12.png’),
array(‘weapon’ => ‘AK-47’, ‘name’ => ‘Elite Build13’, ‘look’ => 1, ‘color’ => ‘#D35400’, ‘image’ => ‘assets/img/ak/13.png’)
);

$winitem = $items[array_rand($items)];
$winpos = array(29 => array(-37, -1), 30 => array(1, 37), 31 => array(38, 74));
$winnr = rand(29,31);
$winpos = $winpos[$winnr];
$tape = ”;

for($i=1;$i<=34;$i++){
if($winnr == $i){
$item = $winitem;
}else{
$item = $items[array_rand($items)];
}
$tape .= ‘<div><div class=”image”><img src=”‘.$item[‘image’].'” alt=”” /></div><div class=”info” style=”background: ‘.$item[‘color’].’;”>’.$item[‘weapon’].'<br />’.$item[‘name’].'</div></div>’;
}

echo json_encode(array(‘tape’ => $tape, ‘dtime’ => rand($winpos[0], $winpos[1]), ‘item’ => $winitem));

?>[/code]

Sincerely, Martijn

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 15.2016 — What problem or problems are you having? Is there an error message? Does it not do something it's supposed to do? Does it do something it is not supposed to do?
Copy linkTweet thisAlerts:
@MordorKing78authorFeb 19.2016 — What problem or problems are you having? Is there an error message? Does it not do something it's supposed to do? Does it do something it is not supposed to do?[/QUOTE]

no error message, I just want to turn this code into have percentages of chance for each item, currently they have the same chance
Copy linkTweet thisAlerts:
@NogDogFeb 19.2016 — I'm thinking you could add a "chance" element to each sub array, and give it an integer value from 1 to n. Then to get a random item, something like
[code=php]
$chances = array();
foreach($items as $ix => $data) {
for($n=1; $n<=$data['chance']; $n++) {
$chances[] = $ix;
}
}
shuffle($chances);
$key = array_pop($chances); // use $key to access $items now
[/code]
×

Success!

Help @MordorKing78 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...