Log in

View Full Version : Odd-Even Determination



marain
06-02-2012, 02:59 PM
$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.

djr33
06-02-2012, 03:14 PM
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.

marain
06-02-2012, 08:43 PM
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.)

djr33
06-03-2012, 02:40 AM
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.

ApacheTech
06-05-2012, 11:02 PM
My answer to this will be in two parts. First of all, finding out is a number is odd or even:


<?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

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."),

);

$rndIndex= rand(0, (count($quotations) -1));

?>


Usage:


<?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:



<blockquote class="quotation">
<?php echo $quotations[$rndIndex]->Quote; ?>
<span id="author">
<?php echo $quotations[$rndIndex]->Author; ?>
</span>
</blockquote>

djr33
06-05-2012, 11:09 PM
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.

ApacheTech
06-05-2012, 11:39 PM
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.