Activity Stream
48,167 MEMBERS
6720 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32
  1.     
    #1
    Respected Developer
    Website's:
    wrzc.org

    Default [PHP] Random Number Problem

    Basically I want to generate a Random number but increase the chances of lower numbers appearing more.

    if I just use mt_rand(1, 1000) the chances of getting the number 1 is the same as 468 which is the same as 1000.

    I want to increase the chances of lower numbers appearing.

    This poor way shows what I mean

    PHP Code: 
    if (mt_rand(12) == 1)   // 50-50 chance
    {
        
    mt_rand(110);
    }
    else if (
    mt_rand(12) == 1)
    {
        
    mt_rand(10100);
    }
    else
    {
        
    mt_rand(1001000);

    So the chance of the number between 1 and 10 is higher than between 10 and 100 which is higher than 100 and 1000.

    It's still pretty crap though as the chance of getting 124 is the same as 975 (not to mention it's generating upto 5 times to get one result).

    I want a gradual increase in chance. I don't even know the mathematical term. Searching "php random sliding scale" gives random image scripts and "php random heavy slide" gives articles on the difference between rand() and mt_rand()

    Can anyone help with an fast efficient function or even tell me the mathematical term for what I want to make searching easier?
    Mr Happy Reviewed by Mr Happy on . [PHP] Random Number Problem Basically I want to generate a Random number but increase the chances of lower numbers appearing more. if I just use mt_rand(1, 1000) the chances of getting the number 1 is the same as 468 which is the same as 1000. I want to increase the chances of lower numbers appearing. This poor way shows what I mean if (mt_rand(1, 2) == 1) // 50-50 chance { Rating: 5
    Tutorial How to SEO your Warez Site a guide to help you increase your organic traffic

    Huge list of Warez Sites and free Multiposter Templates

  2.   Sponsored Links

  3.     
    #2
    ლ(ಠ益ಠლ)
    Website's:
    extremecoderz.com
    hmmm. i have an idea.

    lemme have a go.

  4.     
    #3
    Respected Developer
    PHP Code: 
    $num = (time() & 1) ? mt_rand(110) : mt_rand(11000); 
    Or Maybe

    PHP Code: 
    $num = (time() & 1) ? mt_rand(110) : mt_rand(1mt_rand(101000)); 

  5.     
    #4
    ლ(ಠ益ಠლ)
    Website's:
    extremecoderz.com
    a bit more complicated, but you get the idea. Sorry, i dont know php, but you can of course see the logic.

    PHP Code: 
    static void DoWork()
            {
                
    Random rnd = new Random(5000);
                
    string output string.Empty;

                
    int num1 rnd.Next();
                
    int num2 num1;
                
    num1 rnd.Next();

                if (
    num1 num2)
                {
                    for (
    int i 05i++) // 5 chances to be smaller than previous.
                    
    {
                        
    num1 rnd.Next();
                        if (
    num1 num2)
                            break;
                    }
                }

                
    output += num1.ToString() + Environment.NewLine;
            } 

  6.     
    #5
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    PHP Code: 
    if(substr(time(),0,-1) > 7)
    {
        
    //LOWER NUMBER (Chances are 7/10) of being lower
    }else
    {
       
    //Higher

    But the problem with the above is it will be lower for every 7 out of 10 seconds.
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  7.     
    #6
    ლ(ಠ益ಠლ)
    Website's:
    extremecoderz.com
    why am i the only one writing the whole thing makes me look bloated

  8.     
    #7
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    PHP Code: 
    function doWork()
    {
        
    $rnd rand(0,5000);

        
    $num1 $num2 $rnd+1;


        if((
    $num1+1) > $num2)
        {
            for(
    $i=0;$i<5;$i++)
            {
                
    $num1 $rnd+1;
            }
        }
        return 
    $num1;

    Translated version for JayFella.
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  9.     
    #8
    Respected Developer
    Quote Originally Posted by jayfella View Post
    why am i the only one writing the whole thing makes me look bloated
    My 1 line of code is the whole thing . It has 50% chance of generating a number between 1 and 10 and 50% chance of generating one between 1 and 1000 (using the 1st one). And its fast because its only 1 binary AND operation, one if-else and one rand().

  10.     
    #9
    Respected Developer
    Website's:
    wrzc.org
    Ok so these are all far superior on what I used as an example but their's no way of doing this so
    1 is bigger than 2 which is bigger than 3 which is bigger than 4 which is bigger than 5 .....

    I asked a friend (Phd Mathematical Student) after making this topic and said I need to set up a matrix and add a curve or something where x=0 and y=1000 where points were picked but I didn't really understand what he was saying.

    Anyway it's not that important as long as it's roughly weighted which we've all got. Just though their was a maths way. Maths is seriously not my strong point.

    See while Hyperz way is fast the chances of getting 116 are the same as 959 which isn't exactly what I want.


    Thanks for all the quick replies.
    Tutorial How to SEO your Warez Site a guide to help you increase your organic traffic

    Huge list of Warez Sites and free Multiposter Templates

  11.     
    #10
    Respected Developer
    Oh I didn't think of a matrix, that would actually make it fairly effective and also easy to implement. I'll write another example with once I finish my coffee.

Page 1 of 4 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Problem with number of posts at homepage
    By bdr111446 in forum Wordpress
    Replies: 0
    Last Post: 21st May 2012, 03:10 PM
  2. Problem on DLE 9.3 Error Number: 1267
    By Devils in forum Technical Help Desk Support
    Replies: 6
    Last Post: 26th Sep 2011, 04:17 PM
  3. From random to more random
    By dannym23 in forum General Discussion
    Replies: 4
    Last Post: 13th Aug 2011, 07:35 PM
  4. DLE random log out problem
    By seraphim in forum Webmaster Discussion
    Replies: 0
    Last Post: 12th May 2011, 01:36 PM
  5. How to get those random ads?
    By DXS in forum Webmasters, Money Making
    Replies: 3
    Last Post: 8th Mar 2009, 08:13 AM

Tags for this Thread

BE SOCIAL