Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8608221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:33:06+00:00 2026-06-12T03:33:06+00:00

I understand that this question has likely been asked, but I don’t understand the

  • 0

I understand that this question has likely been asked, but I don’t understand the prior questions enough to know if they do what I want.

$fruits = array('20' => 'apple', '40' => 'orange', '40' => 'pear');

The keys are percentages of the chance of value getting picked. I would like to mt_rand() a number between 0 and 99 and return a value from $fruits based on those percentages.

It’s very possible I’m so confused because I don’t know how to explain what I’m looking for.

Thank you in advance for the help.

Edit: want a random value from $fruits, based on these chances:

I want a 40% chance of getting an orange, a 40% chance of getting a pear, and an 80% chance of getting an apple.

Edit: To further clarify, since either a lot of the answers got it wrong, (or I just don’t understand their code), I needed a result regardless of what number I pick, not just 20, 40, or 40.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:33:08+00:00Added an answer on June 12, 2026 at 3:33 am

    I think something like this will do what you want:

    sample

    (click the submit button multiple times on the sample to get the code to re-execute)

    $fruits = array('apple' => '20', 'orange' => '40', 'pear' => '40');
    
    $newFruits = array();
    foreach ($fruits as $fruit=>$value)
    {
        $newFruits = array_merge($newFruits, array_fill(0, $value, $fruit));
    }
    
    $myFruit = $newFruits[array_rand($newFruits)];
    

    This creates an array ($newFruits), which is a numerically-indexed array with 100 elements. 20 of those elements are ‘apple’, 40 are ‘orange’, and 40 are ‘pear’. Then we select a random index from that array. 20 times out of 100 you will get ‘apple’, 40 times out of 100 you will get ‘orange’, and 40 times out of 100 you will get ‘pear’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that this question has been asked before, but most of the time
First, I know this question has been asked several times before and that in
Ok guys I know this question has been asked before but I am very
I know this question has been asked but I haven't found any satisfactory answers.
I understand that this question has been raised many times already. But I can't
I know that this question has been asked million times so my apologies. I
I understand this question has been asked before, but I couldn't think of a
I understand that this question has been done to death so I'll apologise in
I know this question has been asked before, I'm just trying to be absolutely
I realize this question has most likely been beaten to death, and that it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.