Archive for category Programming

JavaScript: Get a random number within a range

📅 April 26, 2017
The Math object in JavaScript provides the random() function that returns a floating point value from 0 to up to but not including 1.

What if we want to grab an integer, say, from 1 to 25 inclusive?

We can do this easily by modifying the random result to our liking.

Read the rest of this entry »

Advertisements

,

Leave a comment

Tidbit: Discovering Xubuntu Distribution from PHP

📅 September 24, 2016
In PHP, we can use php_uname to grab information about the operating system that the server is running on.

echo php_uname(‘s’) will give the name of the OS, but this is a general name. When executed from a server running Xubuntu, it returns the string “Linux.” Is this Ubuntu, Kubuntu, Xubuntu, Linux Mint, or…what?

What if we want to get the specific Linux distribution? Is this possible from PHP without performing host OS system calls or executing Bash scripts? Yes.

Read the rest of this entry »

, ,

Leave a comment

php: Get Random Image

📅 February 4, 2016
randimgUsing a similar technique as the random text PHP code, here is a simple code snippet that chooses an image file at random and displays it in a browser.

This is useful for randomizing a banner image on a web page. Each time the browser refreshes, a different banner image (should) appear. While the same effect can be performed using JavaScript, what if JavaScript is disabled in the browser?

By moving the code to the server side, a random banner image (or any kind of image) will appear in order to add freshness to a page.

Read the rest of this entry »

, ,

Leave a comment

php: Get Random Text

📅 February 3, 2016
img-php-randquoteHere is a simple but fun PHP code snippet that displays one of several text strings chosen at random to add an element of surprise to a page.

First of all, a working PHP server is required in order to process PHP scripts. This could be a virtual machine using VirtualBox, for example, or something else on any platform. This short tutorial assumes that you have one set up and working.

 

 

Read the rest of this entry »

, ,

Leave a comment

Python 3 Quick Code: Get a Random Item from a List with an Even Chance of Being Chosen

📅 June 12, 2015
py001In Python 3, suppose we want to get a random item from a list that contains duplicates, but we do not want to favor the duplicates.

For example, if there are 3 duplicate items in a list of 10, then there is a 3x probability of obtaining that particular item compared to a single occurrence of that item in the list.

The idea is that by eliminating the duplicates, all items should have an equal chance of being chosen. Here is one strategy to achieve this using a set.

Read the rest of this entry »

,

Leave a comment

SweetFX – Levels

002SweetFX Levels sets new black and white points. This means every pixel whose value is below the black point will be converted into pure black, and every pixel whose value is above the white point will be converted into pure white.

Think of this as the equivalent of level adjustment in GIMP using the histogram. Used sparingly, Levels will trim off excess whiteness, and it will darken shadows and other dark areas that appear too “washed out” when they should be darker.

On the other hand, visual detail is lost when used excessively, and drastic scene changes can be produced. This is either good or bad depending upon the desired effect. In short, Levels is an effect best used for minor touchups to the resulting image.

Read the rest of this entry »

, ,

Leave a comment

SweetFX – Vignette

007SweetFX Vignette darkens the corners of the image to produce a peephole or faded corner effect. This can be used with other effects, such as Sepia and Monochrome, to produce a variation of the old-time photo effect.

Of course, Vignette settings can darken an area greater than the corners alone until the entire image is obscured, so use sparingly for the best results.

Read the rest of this entry »

, ,

Leave a comment