Posts Tagged 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

Changing Filename Extensions – Pattern Matching in Bash

bash1Using bash’s built-in pattern matching ability, we can write a simple script that changes filename extensions. Suppose we have a directory containing over 5000 PNG images, but all image file extensions are mislabeled as .BMP. We want all files to have the .png extension.

Here is a basic script to rename all of the files.

Read the rest of this entry »

, , ,

Leave a comment

Learning a Programming Language

Learning a programming language is rewarding and easier than you might think. If you are new to programming and wish to learn, here are a few tips that help make the learning process more enjoyable and less daunting.
Read the rest of this entry »

,

4 Comments