Random Numbers and NTL

Thursday, December 04th, 2008 9:36pm by Ryan Lewis

According to its website, the Number Theory Library (NTL) is a:

high-performance, portable C++ library providing data structures and algorithms for manipulating signed, arbitrary length integers, and for vectors, matrices, and polynomials over the integers and over finite fields.

I have been using it a lot because of the Cryptography class that I’m in this semester, and something that I need to do frequently is generate random numbers.

To do this, seeding is frequently required, and the NTL is no different, but it took a while for me to actually get it to work. The following will seed the random functions provided by the NTL ZZ class:

ZZ number, seed;
srand(getpid());
seed << ((long) GetTime() ^ rand());
SetSeed(seed);
RandomLen(number, rand() % 300);

Stick that at the top of your main() and you should be all set.

Also, does anyone know of a shorter, possibly easier to remember, way to do this? If so, the comments are open.

Every time you send useless email…

Thursday, November 20th, 2008 1:02am by Ryan Lewis

Please don’t send useless email to the lists…

Samsung LN32A450, nVidia Drivers, and Linux

Wednesday, November 05th, 2008 2:42am by Ryan Lewis

I just recently purchased a Samsung LN32A450 television and wanted to use it as a monitor. I figured that it shouldn’t be too hard, just plug in my computer to it’s VGA port and then change the resolution.

But, when are new things ever easy?

To make a long story short, I had to generate a custom modeline for X to be able to use the TV’s native resolution, 1360×768. I read to use the command cvt 1360 768 60.015Hz, but the returned modeline resulted in a “Mode Not Supported” error from the TV. Instead, I had to use a Windows program called PowerStrip to grab the custom timings that actually worked. Though, the only reason they worked was by luck because Windows didn’t properly detect the monitor either, so I used PowerStrip to add a custom resolution to my registry which was preset with working timings.

Anyway, PowerStrip was nice enough to generate an xorg.conf modeline for me and had to edit the following in my file (this is assuming that you already have the proprietary nVidia drivers already working):

…

Section "Monitor"
    …
    Modeline "1360x768" 85.500 1360 1440 1552 1792 768 771 777 795 +hsync +vsync
EndSection

…

Section "Screen"
    …
    SubSection "Display"
        Depth  24
        Virtual 1360 768
        Modes "1360x768"
    EndSubSection
EndSection

…

Note: any ellipses (…) in the above are just cutting out parts that were not changed in my file.

Basic CouchDB w/ PHP

Monday, August 11th, 2008 2:20pm by Ryan Lewis

I mentioned CouchDB in my last post, but didn’t really elaborate on what it was.

Apache CouchDB is a distributed, fault-tolerant and schema-free document-oriented database accessible via a RESTful HTTP/JSON API. Among other features, it provides robust, incremental replication with bi-directional conflict detection and resolution, and is queryable and indexable using a table-oriented view engine with JavaScript acting as the default view definition language.

I’m gonna show you some really basic ways to use CouchDB using a PHP class found on their website and assume that you have PHP 5.1 or greater and CouchDB 0.8 installed successfully.

The first thing that we need to do is create a sample database for us to play eventually play with using PHP. To manage your databases and documents, you can use Futon, CouchDB’s administrative frontend. It can be accessed at http://localhost:5984/_utils. Create a database called “people” with two documents in it with random IDs that each have two fields in them: “name” and “age”. Also, be sure to enter in some values for those.

Let’s look at this PHP file:

<?php
include_once "CouchDB.php";

$couchdb = new CouchDB("people", "9.57.30.200");
$view = '{ "map": "function(doc) { emit(null, doc); }" }';

try {
  $all = $couchdb->send("/_temp_view", "post", $view)->getBody(true);
}
catch (CouchDBException $e) {
  die("[ERROR]".$e->getMessage()."\n");
}

foreach ($all->rows as $k => $row) {
  echo "Name: ".$row->value->name." / Age: ".$row->value->age."<br/>";
}
?>

This code is using a temporary view to query the database and then simply looping through the returned JSON and printing data from it.  If you set up your database as I mentioned above, $all should be structured like this:

stdClass Object
(
  [total_rows] => 2
  [offset] => 0
  [rows] => Array
  (
    [0] => stdClass Object
    (
      [id] => 4144833052c61ab553875737b88ef91a
      [key] =>
      [value] => stdClass Object
      (
        [_id] => 4144833052c61ab553875737b88ef91a
        [_rev] => 3595404992
        [name] => Ryan
        [age] => 20
      )
    )
    [1] => stdClass Object
    (
      [id] => ff3f20d54772b92aad43c36d6ed4abd1
      [key] =>
      [value] => stdClass Object
      (
        [_id] => ff3f20d54772b92aad43c36d6ed4abd1
        [_rev] => 994830077
        [name] => Bob
        [age] => 40
      )
    )
  )
)

When I visit the page running the script, it will therefore output this:

Name: Ryan / Age: 20
Name: Bob / Age: 40

If anyone wants to see how to do something, just post a comment and I’ll try to whip something up. Also, the CouchDB Wiki has a lot more information if you’re interested.