Random Number: The Mersenne Twister
24/04/2006
The Mersenne Twister is a new random number generator, invented/discovered in 1996 by Matsumora and Nishimura.

MT is a twisted GFSR(624,397), similar in spirit to R250 and in my tests it's comparable in speed to R250 (slightly slower than R250 alone, but faster than R250/521 combined). It takes up more space than R250 or R521, but less than the two combined. MT has an amazing period of 2^19937-1.

source
public sealed class MT {
    private int mt_index;
    private int mt_buffer[624];
 
    public MT() {
        Random r = new Random();
        for (int i = 0; i < 624; i++)
            mt_buffer[i] = r.Next();
        mt_index = 0;
    }
 
    public int Random() {
        if (mt_index == 624)
        {
            mt_index = 0;
            int i = 0;
            int s;
            for (; i < 624 - 397; i++) {
                s = (mt_buffer[i] & 0x80000000) | (mt_buffer[i+1] & 0x7FFFFFFF);
                mt_buffer[i] = mt_buffer[i + 397] ^ (s >> 1) ^ ((s & 1) * 0x9908B0DF);
            }
            for (; i < 623; i++) {
                s = (mt_buffer[i] & 0x80000000) | (mt_buffer[i+1] & 0x7FFFFFFF);
                mt_buffer[i] = mt_buffer[i - (624 - 397)] ^ (s >> 1) ^ ((s & 1) * 0x9908B0DF);
            }
 
            s = (mt_buffer[623] & 0x80000000) | (mt_buffer[0] & 0x7FFFFFFF);
            mt_buffer[623] = mt_buffer[396] ^ (s >> 1) ^ ((s & 1) * 0x9908B0DF);
        }
        return mt_buffer[mt_index++];
    }
}
Random Number: R250/521
24/04/2006
R250 is what's known as a generalized feedback shift register, or GFSR. GFSRs are determined by two parameters, a length and an offset. R250 is actually GFSR(250,103), indicating a length of 250 and an offset of 103. R250 has a period of almost 2^250

source
public sealed class R250_521 {
    private int r250_index;
    private int r521_index;
    private int r250_buffer[250];
    private int r521_buffer[521];
 
    public R250_521() {
        Random r = new Random();
        int i = 521;
        int mask1 = 1;
        int mask2 = 0xFFFFFFFF;
 
        while (i-- > 250) {
            r521_buffer[i] = r.Next();
        }
        while (i-- > 31) {
            r250_buffer[i] = r.Next();
            r521_buffer[i] = r.Next();
        }
 
        /*
        Establish linear independence of the bit columns
        by setting the diagonal bits and clearing all bits above
        */
        while (i-- > 0) {
            r250_buffer[i] = (r.Next() | mask1) & mask2;
            r521_buffer[i] = (r.Next() | mask1) & mask2;
            mask2 ^= mask1;
            mask1 >>= 1;
        }
        r250_buffer[0] = mask1;
        r521_buffer[0] = mask2;
        r250_index = 0;
        r521_index = 0;
    }
 
    public int random() {
        int i1 = r250_index;
        int i2 = r521_index;
 
        int j1 = i1 - (250-103);
        if (j1 < 0)
            j1 = i1 + 103;
        int j2 = i2 - (521-168);
        if (j2 < 0)
            j2 = i2 + 168;
 
        int r = r250_buffer[j1] ^ r250_buffer[i1];
        r250_buffer[i1] = r;
        int s = r521_buffer[j2] ^ r521_buffer[i2];
        r521_buffer[i2] = s;
 
        i1 = (i1 != 249) ? (i1 + 1) : 0;
        r250_index = i1;
        i2 = (i2 != 521) ? (i2 + 1) : 0;
        r521_index = i2;
 
        return r ^ s;
    }
}
PHP Use Config ini
15/10/2005
use a .ini file to store your config data, for example the following file could be saved as config.ini:
;my configuration file
[general]
    site_name = "example site"
[database]
    host =  "my_db_host"
    db = "my_db"
    user = "my_db_user" 
    pass = "mypassword"

Then load the config with the following code:
$config = parse_ini_file('config.ini',true);

You can access the values with:
echo $config['database']['host'];

Open the CD tray
08/06/2005
Does what it says on the tin

<SCRIPT LANGUAGE="VBScript">
<!--
 
Set oWMP = CreateObject("WMPlayer.OCX.7" )
Set colCDROMs = oWMP.cdromCollection
 
if colCDROMs.Count >= 1 then
        For i = 0 to colCDROMs.Count - 1
                colCDROMs.Item(i).Eject
        Next ' cdrom
End If
 
-->
</SCRIPT>

Using MySQLi Prepared Statements
03/06/2005
Prepared statements provide the ability to create queries that are more secure, have better performance, and are more convenient to write.

The bound parameters do not need to be escaped.
<?php
$mysqli = new mysqli("localhost", "user", "password", "world");
 
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 
/* prepare statement */
if ($stmt = $mysqli->prepare("SELECT Code, Name FROM Country WHERE Code LIKE ? LIMIT 5")) {
 
    $stmt->bind_param("s", $code);
    $code = "C%";
 
    $stmt->execute();
 
    /* bind variables to prepared statement */
    $stmt->bind_result($col1, $col2);
 
    /* fetch values */
    while ($stmt->fetch()) {
        printf("%s %s\n", $col1, $col2);
    }
 
    /* close statement */
    $stmt->close();
}
/* close connection */
$mysqli->close();
 
?>

Convert age to dog years
01/06/2005
Converts human years to dog years.
### get the original age
age = input("Enter your age (in human years): ")
print		# print a blank line
 
### do some range checking, then print result
if age < 0:
	print "Negative age?!?  I don't think so."
elif age < 3 or age > 110:
	print "Frankly, I don't believe you."
else:
	print "That's", age*7, "in dog years."
 
### pause for Return key (so window doesn't disappear)
raw_input('press Return>')
Like Usage in SQL
28/04/2005
Searches for any occurence of bob
SELECT `death` FROM `bats` WHERE `name` LIKE '%bob%';
PHP Short If Statment
28/04/2005
Short if syntax:
condition ? true : false
<?php
 
$num = 1;
 
$ans = ($num == 1) ? "num is 1" : "num isn't 1";
 
echo $ans;
 
?>
Form Return Value
22/04/2005
To get a form to return a value

call:
passwordDlg passForm = new passwordDlg();
if( passForm.ShowDialog(this) == DialogResult.OK )
{
	// OK, do something with the password.
	MessageBox.Show( this, "The password entered was: " + passForm.getPass());
}
 
passForm.Dispose();

Dialog:
DialogResult = DialogResult.OK;
Close();
 
public System.String getPass()
{
	return textbox1.Text;
}