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

  • Home
  • SEARCH
  • 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 314747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:13:42+00:00 2026-05-12T08:13:42+00:00

Is this how PHP implemented random number generating? Say I want to calculate a

  • 0

Is this how PHP implemented random number generating?

Say I want to calculate a yes or a no. Everytime I have a certain probability percentage (say: 0,05% for this example).

I do:

$possibilities = 100 / $probabilityPercentage; //$possibilities = 2000
$yes = rand(1,$possibilities);

$yesCheck = $possiblities;            //OPTION 1
$yesCheck = rand(1,$possibilities);   //OPTION 2


($yesCheck == $yes) ? return true : return false;

Does it give the same results with either option?

  • 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-12T08:13:42+00:00Added an answer on May 12, 2026 at 8:13 am

    Let the data speak for itself.

    Code

    vinko@parrot:~$ more rand.php
    <?php
    
    $randrandsum = 0;
    $randconstsum = 0;
    $count = 20;
    for ($j = 0; $j < $count; $j++) {
            $randrand = 0;
            $randconst = 0;
            for ($i = 0; $i < 10000000; $i++ ){
                    $a = rand(1,1000);
                    $b = rand(1,1000);
                    if ($a == $b) $randrand++;
            }
            for ($i = 0; $i < 10000000; $i++ ){
                    $a = rand(1,1000);
                    $c = 1000;
                    if ($c == $a) $randconst++;
            }
            $randrandsum += $randrand;
            $randconstsum += $randconst;
            print ($j+1)." RAND-RAND: $randrand RAND-CONST: $randconst\n";
    }
    print "AVG RAND-RAND: ".($randrandsum/$count);
    print " AVG RAND-CONST: ".($randconstsum/$count)."\n";
    ?>
    

    Test run

    vinko@parrot:~$ php rand.php
    1 RAND-RAND: 10043 RAND-CONST: 10018
    2 RAND-RAND: 9940 RAND-CONST: 10132
    3 RAND-RAND: 9879 RAND-CONST: 10042
    4 RAND-RAND: 9878 RAND-CONST: 9965
    5 RAND-RAND: 10226 RAND-CONST: 9867
    6 RAND-RAND: 9866 RAND-CONST: 9992
    7 RAND-RAND: 10069 RAND-CONST: 9953
    8 RAND-RAND: 9967 RAND-CONST: 9862
    9 RAND-RAND: 10009 RAND-CONST: 10060
    10 RAND-RAND: 9809 RAND-CONST: 9985
    11 RAND-RAND: 9939 RAND-CONST: 10057
    12 RAND-RAND: 9945 RAND-CONST: 10013
    13 RAND-RAND: 10090 RAND-CONST: 9936
    14 RAND-RAND: 10000 RAND-CONST: 9867
    15 RAND-RAND: 10055 RAND-CONST: 10088
    16 RAND-RAND: 10129 RAND-CONST: 9875
    17 RAND-RAND: 9846 RAND-CONST: 10056
    18 RAND-RAND: 9961 RAND-CONST: 9930
    19 RAND-RAND: 10063 RAND-CONST: 10001
    20 RAND-RAND: 10047 RAND-CONST: 10037
    AVG RAND-RAND: 9988.05 AVG RAND-CONST: 9986.8
    

    Given the above results I’d say that for all practical purposes, both options are equivalent, giving the expected 1/1000 result on both cases.

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

Sidebar

Ask A Question

Stats

  • Questions 148k
  • Answers 148k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This will achieve the same effect as @Chii's answer: <c:set… May 12, 2026 at 9:21 am
  • Editorial Team
    Editorial Team added an answer Well if you don't have any synchronization applied at all… May 12, 2026 at 9:21 am
  • Editorial Team
    Editorial Team added an answer Do you have firebug installed so you can test to… May 12, 2026 at 9:21 am

Related Questions

Why would you do an automatic HTML post rather than a simple redirect? Is
I hope I'm not writing a duplicate, but I haven't found anything that answers
How can I make a table sortable using javascript without loading data via AJAX?
Okay, the question might seem dumb, but I'm asking it anyways. After struggling for
I'm been a PHP developer for quite some time now but until today I've

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.