Results 1 to 7 of 7

Thread: Odd-Even Determination

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Smile Odd-Even Determination

    $quotations is an array that consists of quotation pairs. By "pairs," I mean that each even element, beginning with 0, is a quotation, and each immediately following odd element is the quotation author. What I wanted to do was this: Each time the user visited or refreshed the page, I wanted to display a randomly selected quotation and quotation author, from the array.

    The rand function was the obvious tool. However, the rand function could be expected to return an odd number half the time, which would then point to an author, followed by an unrelated quotation, as opposed to a quotation followed by its author. A pox on that!

    Numerous methods exist to determine whether a number is odd or even and, depending on the result, adjust accordingly. One way, for example, is

    if ($number & 1) {

    $number--;

    }

    However, a more elegant method exists. This method does not require testing at all. Simply "and" the low-order bit with zero. If it was even, it stays even. If it was odd, you make it even:

    [CODE]

    $quoteCount = count($quotations);
    $bigIndex = rand(0, ($quoteCount -1));
    $bigIndex = $bigIndex &254;
    echo "<br /><br /> {$quotations [$bigIndex]} <br /><br /><div align='right';> {$quotations [$bigIndex + 1]} </div>
    <br />";

    [CODE]

    Just thought I'd share.

    A.
    Last edited by marain; 06-02-2012 at 08:55 PM. Reason: To correct sybtax error in echo statement: Original post had single quotes within single quotes

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Why not multiply it by two?

    $bigIndex = rand(0, round($quoteCount -1)/2))*2;

    (You would also need to then set the maximum random value to half of what it was originally and round that also.)


    But personally I'd arrange the array in a better way. Use the author as the array key and the quotation as the value. Or (if you might have two from the same person) you could use a multidimensional array-- each entry is an array containing two parts, the author and the quote.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Smile

    Well, not all of the quotations have authors (known to me). For example...

    "Mrs. Claypool: But Professor Wagstaff, if we tear down the dormitories, where will the students sleep?
    <br />
    <br />Groucho Marx (as Professor Wagstaff): Where they always slept. In the classroom.",

    "&nbsp;",

    The author was perhaps Groucho, but I'm inclined to just let the quote speak for itself. (The array contains other similar situations.)

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The math above should still work. (Multiplying by two.)
    And my second option would allow the option of leaving one (or even both) of the arguments blank-- using a multidimensional array.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    My answer to this will be in two parts. First of all, finding out is a number is odd or even:

    PHP Code:
    <?php
    function isEven($n) {
        if (
    $n 2) {
            return 
    true;
        } else {
            return 
    false;
        }
    }
    ?>
    The second is to do with the question. I'd run it as an array or collection of quotation objects:

    PHP Code:
    <?php

    class Quotation {

        private 
    $author;
        private 
    $quote;

        function 
    __construct($author$quote) {
            
    $this->author $author;
            
    $this->quote $quote;
        }

        public function 
    Author {
            return 
    $this->author;
        }

        public function 
    Quote{
            return 
    $this->quote;
        }
    }

    $quotations = array(

        new 
    Quotation("Albert Einstein""Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe."),
        new 
    Quotation("Bill Gates""The Internet is becoming the town square for the global village of tomorrow."),
        new 
    Quotation("Gandalf""Use the force, Harry!"),
        new 
    Quotation("Unkown Author""No-one knows who wrote this quote but the author."),

    );

    $rndIndexrand(0, (count($quotations) -1));

    ?>
    Usage:
    PHP Code:
    <?php foreach ($quotations as $quotation) { ?>
        <blockquote class="quotation">
            <?php echo $quotation->Quote?>
            <span id="author">
                <?php echo $quotation->Author?>
            </span>
        </blockquote>
    <?php ?>
    Or for a random quote:

    PHP Code:
    <blockquote class="quotation">
        <?php echo $quotations[$rndIndex]->Quote?>
        <span id="author">
            <?php echo $quotations[$rndIndex]->Author?>
        </span>
    </blockquote>
    Last edited by ApacheTech; 06-05-2012 at 11:20 PM.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That would work, but I don't see why you'd want OOP in this case. And it doesn't solve the other problem that the OP said sometimes one of the values (author) is missing.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    I suspect in that case, you'd want something to display. So you'd put "Unknown Author" as the author, as in $quotations[3] in the example. Or you can set it as "".

    new Quotation("", "Some quote from an unknown author");

    You could set this up as a standard multi-dimensional array, I've always found it better to use OOP; it's easier to work with, especially with large projects.

Bookmarks

Posting Permissions

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