# Randomness in CSS

The majority of the languages have some mechanisms for generating random numbers. Unfortunately, that is not the case in CSS. This might not be a problem for most websites, but when dealing with a more generative approach (which CSS is really great for) we have to resort to JavaScript to generate random values. In his article, I will explore a nice way of generating it using only CSS.I will cover the following topics:Generating random numbers in CSSUsing random values to generate ranges of numbers and coloursGenerating effect like in the cover for this articleTo generate random values in CSS we need first to understand how the randomness in computers works. Computers cannot come up with a random number by themselves — they were designed to work in a very rigorous way — for a given input they return a set output. All they can do is generate pseudo-random numbers — the sequence of numbers that looks like it’s random but which still has some underlying rules behind it.An example of it might be the following function:f(x) = (A*x + c) mod 1;Aand c parameters can be adjusted to our liking. mod 1 is a notation meaning that we want to take a non-integer reminder of the number, for example 5.3 mod 1 = 0.3.The function will always give us results within the range of 0-1 which is handy because we can later scale it however we want without needing to change the function itself – when generating colours we will use different ranges when generating sizes in pixels.Let’s set A=8.385, c=2.534 then we will get the following pseudo-random sequence:fn(0) = 0.5339999999999998fn(1) = 0.9190000000000005fn(2) = 0.3039999999999985fn(3) = 0.6890000000000001fn(4) = 0.07399999999999807fn(5) = 0.4589999999999961fn(6) = 0.8440000000000012fn(7) = 0.2289999999999992fn(8) = 0.6140000000000043fn(9) = 0.9990000000000094fn(10) = 0.38400000000000034If you don’t know the underlying sequence the numbers generated by the function might look random.In this case, given enough values, it is easy to reverse engineer the function, but the real-life random functions are usually much more complex. You can use higher order polynomials, trigonometry function and whatever else you imagine… unless you are working in plain CSS.In CSS we do not have a big range of mathematical functions to operate on but we can still use some basic operations like sum and multiplication, and using @property we can perform floor, but there’s not much more than that. Fortunately, that’s more than enough to get satisfactory results.In our CSS random number generator, we will use the same technique but, to make the function less predictable, we will use a second-degree polynomial — meaning we will include an extra component of B*x^2.Moreover, our example will use sequential numbers to generate our random numbers. CSS does not natively offer us a mechanism to easily get the sequential numbers for each of the elements but, using the technique from my last article, we can generate it. The number will be stored as a –n CSS variable. For more information check out my previous article.Here’s the solution for our random number generator:The CodePen above plots the results of the function for the first 100 values. » Read More

## Like to keep reading?

This article first appeared on medium.com. If you'd like to continue this story, follow the white rabbit.