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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:13:06+00:00 2026-05-21T20:13:06+00:00

I m having a application in which i have to select a number out

  • 0

I m having a application in which i have to select a number out of many numbers according to their weights. Every time I will select , I have send the result to flash.I have found a algorithm in python. I have implemented it in php and was testing for its results. If i was running that algo in python it was giving good results but in php not so good. Ex. (1=>30,2=>40,3=>30) After running many times , the probablity of occurence first number in weighted array is always more but in python it is uniform. I have attatched the PHP code.

define("MAX",100000);
$reelfrequencies=array(30,40,30);
echo weightedselect($reelfrequencies);
/*function weightedselect($frequency)
{ 
  $arr=cumWghtArray($frequency);//array(35,96,100);
  print_r($arr);
  $len=sizeof($frequency);
  $count=array();
  echo $r=mt_rand(0,$arr[$len-1]);
  $index=binarysearch($arr,$r,0,$len-1);
  return $index;
}*/
function cumWghtArray($arr)
{
  $cumArr=array();
  $cum=0;
  $size=sizeof($arr);
  for($i=0;$i<$size;$i++)
  {
    $cum+=$arr[$i];
    array_push($cumArr,$cum);
  }
  return $cumArr;
}

function weightedselect($frequency)
{
  $arr=cumWghtArray($frequency);//array(35,96,100);
  $len=sizeof($frequency);
  $count=array();
  $count[0]=$count[1]=$count[2]=0;
  for($i=0;$i<MAX;$i++)
  {
    $r=mt_rand(0,$arr[$len-1]);
    $index=binarysearch($arr,$r,0,$len-1);
    $count[$index]++;
  }
  for($i=0;$i<3;$i++)
  {
    $count[$i]/=MAX;
    echo $i." ".$count[$i]."\n";
  }
}
function binarySearch($ar,$value,$first,$last)
{
  if($last<$first)
    return -1;
  $mid=intVal(($first+$last)/2);
  $a=$ar[$mid];
  if($a===$value)
    return $mid;
  if($a>$value&&(($mid-1>=0&&$ar[$mid-1]<$value)||$mid==0))
    return $mid;
  else if($a>$value)
    $last=$mid-1;
  else if($a<$value)
    $first=$mid+1;
  return binarySearch($ar,$value,$first,$last);
}

Here is the Python Code. I have taken this code from this forum .
import random
import bisect
import collections

def cdf(weights):
    total=sum(weights)
    result=[]
    cumsum=0
    for w in weights:
        cumsum+=w
        result.append(cumsum/total)
    return result

def choice(population,weights):
    assert len(population) == len(weights)
    cdf_vals=cdf(weights)
    x=random.random()
    idx=bisect.bisect(cdf_vals,x)
    return population[idx]

weights=[0.30,0.40,0.30]
population="ABC"
counts={"A":0.0,"B":0.0,"C":0.0}
max=10000
for i in range(max):
    c=choice(population,weights)
    counts[c]=counts[c]+1
print(counts)
for k, v in counts.iteritems():
    counts[k]=v/max
print(counts)

Problem is of mt_rand() function which is not uniform. The python random.rand() is very much uniform. Which random function should i implement in php with a proper seeding value every time it runs. I was thinking of using Withcmann (used by python random.random) but how will i provide the seed.

  • 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-21T20:13:07+00:00Added an answer on May 21, 2026 at 8:13 pm

    Both rand and mt_rand should both be more than sufficiently random for your task here. If you needed to seed mt_rand you could use mt_srand, but there’s no need since PHP 4.2 as this is done for you.

    I suspect the issue is with your code, which seems unnecessarily involved given what I believe you’re trying to do, which is just pick a random number with weighted probabilities.

    This may help: Generating random results by weight in PHP?

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

Sidebar

Related Questions

I have the following application which replicates an issue I'm having in a larger
Scenarion: I am having a web application which is going to use SqlServerReport(SSRS) sitting
I'm having trouble figuring out the best approach for an certain application. I'm not
I have got myself saddled with an existing application - which attempts do do
I have a GWT application which I would like to run from within a
Suppose I have logged into an application which is running from IIS . Now
I have made some changes to a Classic ASP application which breaks foreign letters
I have an iPhone application which is, in essence, a list. There is a
I have a UITabBar application, which has three tabs. The first tab has a
Hi I am building a simple application that will have a combo box to

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.