Posts Tagged ‘math’

Binary Code and Bitwise Operators (in PHP)

Sunday, April 6th, 2008

At it’s core, all of the information on your computer is made up of bits – or 0’s and 1’s. There’s quite a bit of interpretation that goes on between that basic binary code and the information as it is displayed on your screen.

However, you may find a time to work with data at the binary level in PHP (and many other programming languages). Here’s a quick guide to understanding binary numbers and the common operators for handling them.
(more…)

Combinations: General Function to Get All Possible Combinations

Thursday, February 28th, 2008

Previously, we looked at how to calculate a binomial coefficient (to find the total number of possible combinations) and an example of how to generate a list of combinations with hard-coded loops.

A function would be much more useful to us if it was flexible – if it could generate a list of combinations for any size set of numbers. We can’t do this with the hard coded loop solution, so we need to come up with a better method.
(more…)

How to Generate a List of Possible Combinations

Tuesday, February 26th, 2008

In statistics and probability, the combination is something special. There are tons of problems that revolve around how many combinations you can get choosing a certain number of random items from a set.

We’ve developed mathematical formulas to help us determine the number of possible combinations – and therefore the possibility of getting any single combination – but how can we generate a list of the actual combinations?
(more…)

Getting Combinations: Calculating a Binomial Coefficient

Sunday, February 24th, 2008

I came across this problem on the DigitalPoint forums the other day. How can we write a script to calculate the total number of random combinations in a set (i.e. the binomial coefficient), and how can we generate a list of these combinations?

Today, we’ll tackle the first part. We’ll start with a quick primer on math – what is a binomial coefficient? Then, we’ll look into the most efficient way to calculate that and get the total number of possible combinations.
(more…)

Javascript Function: Random Number Generator

Sunday, February 24th, 2008

Yesterday, I wrote a short article on generating random numbers in Javascript. The Javascript random number generator is similar to C/++ – it gives you a random number from 0 to 1 and you need to make it useful.

At the suggestion of a reader (thanks Justin), I decided to go ahead and turn the general formula for this into a short helper function to generate a random number.
(more…)

How to Create a Random Number in Javascript

Saturday, February 23rd, 2008

I’ve been working with PHP for some time now, and I’ve become spoiled with the rand() function for generating random numbers. It’s so simple – pass a min, pass a max, and get the number you want.

While I was working on various game-related scripts this weekend, I was left wondering how to create a random number in Javascript. It’s not quite as simple as the rand() function – we’ll have to go back to the old C-style random number generation.
(more…)