Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Random Help

  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Random Help

    I'm trying to learn how to create a random script, but I want the random script to to only include specific numbers, and then generate a random value of one those numbers.

    Essentially, I want values, of 1, 5, 8, 17, 23, 100, 41 etc. And then to randomly generate a number.

    Eventually, I'll look to weight the probability of the number being randomly selected.

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Well, I learned I can do this to generate a random number of specific values. But I'm not sure yet how to possibly weight any of numbers.
    PHP Code:
    $numbers = array ("4""6""8""10""12""15""20""25""50""100");

    srand ((double) microtime() * 1000000);
    $randomnumber rand(0,count($numbers)-1);

    echo 
    "<p>" $numbers[$randomnumber] . "</p>"

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Use array_rand(), you can seed it if you want.

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:

    $number 
    = array(1,4,5,6,7,8,22,12,34,45,2);
    $i rand(0arraymax);
    $shuffle shuffle($number);
    echo 
    "$shuffle[$i]"
    that would show a random number from the array although change $arraymax to how many numbers are in your array -1 (because array starts at 0)

  • #5
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    use:
    PHP Code:
    rand(); 

  • #6
    Regular Coder goughy000's Avatar
    Join Date
    Nov 2005
    Location
    England
    Posts
    415
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by the-dream View Post
    use:
    PHP Code:
    rand(); 
    he wants a pre defined set of values that will be chosen from at random so this wont work

    http://uk.php.net/array_rand is your best bet
    PHP Freelancing!
    ByteForums!
    Scriptlance.comNeed a job done quick? post it on scriptlance! OR! your a programmer? sign up and get paid for these jobs!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •