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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:30:06+00:00 2026-06-17T02:30:06+00:00

I really feel ashamed to ask this question however I don’t have time for

  • 0

I really feel ashamed to ask this question however I don’t have time for revision. Also not a native English speaker, so excuse my lack of math vocabulary.

I am writing a program that requires assigning probabilities to variables then selecting one randomly.

Example:

Imagine that I have I coin, I would like to assign the probably of 70% to heads and 30% to tails. When I toss it I would like to have 70% chance that the heads appears and 30% tails.

A dumb way to do it is to create an array of cells insert the heads 70 cells and the tail in 30. Randomize them and select one randomly.

Edit 1: I also would like to point out that I am not limited to 2 variables. For example lets say that I have 3 characters to select between (*,\$,#) and I want to assign the following probably to each of them * = 30%, \$ = 30%, and # = 40%.

That’s why I did not want to to use the random function and wanted to see how it was done mathematically.

  • 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-17T02:30:07+00:00Added an answer on June 17, 2026 at 2:30 am

    Here is a simple function to calculate weighted rand:

    <?php
    function weightedRand($weights, $weight_sum = 100){
        $r = rand(1,$weight_sum);
        $n = count($weights);
        $i = 0;
        while($r > 0 && $i < $n){
            $r -= $weights[$i];
            $i++;
        }
        return $i - 1;
    }
    

    This function accepts an array. For example array(30,70) will have 30% chance getting 0 and 70% chance getting 1. This should work for multiple weights.

    Its principle is to subtract the generated random number by the weight until it gets less than or equal to zero.

    Demo with 30%:70%
    Demo with 20%:30%:50%

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

Sidebar

Related Questions

Well! I feel really stupid for this question, and I wholly don't mind if
I feel like this might be really simple but I'm just not getting it
I feel like there's a simple solution to this, but I'm not really sure
(Really strugging to title this question, so if anyone has suggestions feel free.) Say
First let me say that I really feel directionless on this question. I am
I have spent days working on this and really feel dumb. I have been
I feel really silly asking this, but it is completely blowing my mind why
Ok I feel really dumb asking this but I seem to be missing something
I suspect I am going to feel really stupid after posting this but here
I really want to get this to work, but I feel like I've been

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.