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 6346633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:03:20+00:00 2026-05-24T21:03:20+00:00

I am trying to figure out how to shuffle an array of URL’s based

  • 0

I am trying to figure out how to shuffle an array of URL’s based on a percentage so that each URL get’s picked a certain number of times.

<?php

  $urls = array(
  'http://www.google.com'=>'25%', 
  'http://www.yahoo.com'=>'25%',
  'http://www.bing.com' =>'50%');

I thought about going the rand() route and just getting a random number between 1-100 and making a switch statement with a bunch of ranges, but that seems less than elegant and clumsy. I also don’t know how reliable doing this would be. I am trying to get as close to perfect as possible. If I shuffle through 100 times I am not sure most of the rand() examples would list the above array with google.com and yahoo.com getting selected 25 times each (+/- 2) and bing.com getting selected 50 times.

Is their a way to get accurate weighted shuffling? Thanks

  • 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-05-24T21:03:20+00:00Added an answer on May 24, 2026 at 9:03 pm

    You’ll still want a rand. In a distribution of 100, you’re not likely to get a perfectly even 100% distribution. If you want that, you’ll need to store state in a DB, or somehow static to the server itself. In fact, what you would do is just decrement the array values until they’re all 0, then start again.

    Here is a representation of how it would work without the perfect-match distribution (assuming it is in a function):

    $urls = array(
      'http://www.google.com'=>25, 
      'http://www.yahoo.com'=>25,
      'http://www.bing.com' =>50);
    
    // Assuming that totalWeight might not be 100 for some reason.
    $totalWeight = array_sum(array_values($urls)); 
    
    $currentWeight = 0;
    $rand = rand(1,$totalWeight);
    
    foreach ($urls as $key=>$value)
    {
        $currentWeight += $value;
        if ($currentWeight > $rand)
        {
            return $key;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to figure out how to rewrite this url clientside blog.com/post/how-to-get-the-ladies to point serverside
Trying to figure out a way to throw out attributes in this data that
Trying to figure out how to create a method that takes in a list
Trying to figure out why my base64 sha1's are different. I get Password 'a'
I can't figure out what's going on here... I'm trying to copy an array,
Trying to figure out the MVC-concept. I have a NSCustomView that implements drawRect:, which
trying to figure out how to get SQL to read a value as a
Trying to figure out an equation to get the current group a page would
Trying to figure out the best way to have a templated brochure that creates
Trying to figure out how to get the current User's Full Name as entered

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.