+ Reply to Thread
Results 1 to 5 of 5

Thread: More info about the rand() function ?

  1. #1
    cerbere is offline x10Hosting Member cerbere is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    51

    More info about the rand() function ?

    Hello everyone,

    I want to generate random integers in Perl, to be used as
    array indexes. Let's say my array size is 10, so I want to
    get evenly distributed integers between 0 and 9.

    If I use

    $index = int(rand(10));

    is there a chance (however slight) that I will eventually
    get 10 as $index ? That would be out of bounds, and we all
    hate that... Of course, something like

    if($index == 10) { $index = 9; }

    will fix it but it hurts my feelings about elegant programming :>)

    So the bottom line of my question is : can rand(n) ever
    return n in Perl (or PHP) ?

    PS : You can see a distribution of 100000 calls to rand()
    at : http://ixedix.x10hosting.com/
    (hit the "rand() test" button at bottom, then refresh
    to get new distributions)

    PPS : Where is the darn "Close thread" checkbox that the
    sticky message begs us to use ? Can't find it on my browser.

  2. #2
    scopey is offline x10Hosting Member scopey is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    62

    Re: More info about the rand() function ?

    Calling rand() 100000 times will use a fair bit of CPU.

    In php, the syntax for rand() is:

    rand((int) min, (int) max);

    So if I put rand(0,10) there should be a 1/11 chance that it will be 0, and a 1/11 chance that it will be 10.

    Also, the server your site is hosted on is down -.- (absolut?).

    Also, the close thread button is located by clicking edit on your post, clicking 'Go Advanced', then it will be below the textarea. (Edit - Or at the very top of the page.)
    Last edited by scopey; 09-12-2008 at 05:53 AM.
    - When in doubt, refer to the PHP manual.

  3. #3
    cerbere is offline x10Hosting Member cerbere is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    51

    Re: More info about the rand() function ?

    After further reading, I found that rand(expr) returns a
    random number between 0 (inclusive) and expr (EXCLUSIVE).

    So the right way to get uniformly distributed integers
    between 0 and n - 1 in Perl is effectively

    $rand_int = int(rand($n));

    This is guaranteed to NEVER return $rand_int == $n;

    Thanks for the reply about closing a thread, scopey,
    but I can't even find the "edit" button. Maybe I need
    a new browser !

    ADMIN, PLEASE CLOSE THIS THREAD.

  4. #4
    Salvatos's Avatar
    Salvatos is offline x10 Lieutenant Salvatos is an unknown quantity at this point
    Join Date
    Jun 2006
    Location
    Québec, Canada
    Posts
    271

    Re: More info about the rand() function ?

    Yea the edit button has strangely disappeared for me too... Well in former posts, not on this one apparently ^^
    Last edited by Salvatos; 09-14-2008 at 06:17 PM. Reason: Because I can

  5. #5
    vol7ron's Avatar
    vol7ron is offline x10 Lieutenant vol7ron is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    DC
    Posts
    434

    Re: More info about the rand() function ?

    You shouldn't be so hasty to close.


    Quote Originally Posted by cerbere View Post
    After further reading, I found that rand(expr) returns a
    random number between 0 (inclusive) and expr (EXCLUSIVE).
    This is correct. rand(num) generates a range of values between 0 and that number, but not including the number.

    Quote Originally Posted by cerbere View Post
    So the right way to get uniformly distributed integers
    between 0 and n - 1 in Perl is effectively

    $rand_int = int(rand($n));
    Yes and no. It's the only way to do from 0..(n-1). The function does what it's meant to do. Usually, a person would opt to include the upper bound, but not include 0. You might see something like:
    $rand_int = int(rand($n))+1; # random number 1..n

    That way the range of values is 1 to N.
    If you find my post useful please add to my reputation by clicking the +Rep button
    You may also use the Donate link to donate credits - this is appreciated too Thanks to those whom have donated so far!


+ Reply to Thread

Similar Threads

  1. Replies: 4
    Last Post: 07-13-2008, 07:03 AM
  2. wrong account info
    By 3aKaT in forum Feedback and Suggestions
    Replies: 5
    Last Post: 09-21-2006, 05:49 PM
  3. Account Info Not Updating?
    By hahoozhafax in forum Free Hosting
    Replies: 4
    Last Post: 07-31-2006, 06:25 AM
  4. Want my ftp info...'ive lost it
    By justhk in forum Free Hosting
    Replies: 2
    Last Post: 11-02-2005, 09:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers