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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:16:43+00:00 2026-06-04T16:16:43+00:00

I have a fully-populated array of values, and I would like to arbitrarily remove

  • 0

I have a fully-populated array of values, and I would like to arbitrarily remove elements from this array with more removed towards the far end.

For example, given input ( where a . signifies a populated index )

............................................

I would like something like

....... . ...  .. .  .  ..    .          .  

My first thought was to count the elements, then iterate over the array generating a random number somewhere between the current index and the total size of the array, eg:

if ( mt_rand( 0, $total ) > $total - $current_index ) 
    //remove this element

however, as this entails making a random number each time the loop goes round it becomes very arduous.

Is there a better way of doing this?

  • 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-04T16:16:45+00:00Added an answer on June 4, 2026 at 4:16 pm

    One easy way is to flip a weighted coin for each entry with coin flips more weighted towards the end. For example, if the array is size n, for each entry you could choose a random number from 0 to n-1 and only keep the value if the index is less than or equal to the random number. (That is, keep each entry with probability 1 - index/total.) This has the nice advantage that if you’re going to be compacting your array anyways, and you’re using a good enough but efficient random number generator (could be a simple integer hash over a nonce), it’s going to be rather fast for memory access.

    On the other hand if you’re only blanking out a few items and aren’t rearranging the array, you can go with some sort of weighted random number generator that more often chooses numbers that are toward the end of the index. For example, if you have a random number generator that generates floats in the value of [0,1] (closed or open bounds not mattering that much likely), consider obtaining such a random float r and squaring it. This will tend to prefer lower values. You can fix this by flipping it around: 1-r^2. Of course, you need this to be in your index range of 0 to n - 1, so take floor(n * (1 - r^2)) and also round n down to n-1.

    There’s practically an infinite number of variations on both of these techniques.

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

Sidebar

Related Questions

I have table view which is populated from an array grabbed from a url.
I have a javascript array of objects that I would like to use to
Bear with me if this is unclear; I have trouble fully wrapping my head
I have a database table on a development server that is now fully populated
I have an array which have this structure: $queues[n] Array ( [id] => integer
I have a DataTable, fully populated, which I want to set to a DatagridView:
Quick question I have a fully functional database website apart from the filter aspect.
I have a full list that populates a UITableView . This I want to
In Vaadin's website it says that only Eclipse and NetBeans have fully functional (which
I have a fully working Setup project within Visual Studio 2008 that takes inputs

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.