I am working on a project where I need to generate a random value and assign it to a variable…
Initially I was doing this:
var=$(date +%s | sha512sum | base64 | head -c 12 ; echo)
Which was okay until I started executing my script more than once in a short period of time. Suddenly my random variable wasn’t so random and I was getting the same value multiple times in a row. This is a flaw of this method in that it is a hash of the value of the date (represented as an epoch value) at a given second.
UPDATE – 6/28/2016 – Someone pointed out to me that the OpenSSL method below also relies on the value of the date and time. I am not sure why it doesn’t experience the same issues as just using a sha512sum and, as I have a working method, am not going to investigate it further. I just wanted to clarify for the sake of accuracy. I am not an expert with this stuff.
So I had to can that method and that led me to trying this next:
var=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 12 | head -n 1)
This worked perfectly from the shell. Worked great when I executed a test script from the shell using it. However the PHP script that fired off the bash script would freeze whenever I used this. (more…)
I have been learning a ton of PHP lately in an effort to build a significant amount of custom functionality into a Drupal website. As I am not a developer by trade it has been a steep climb upward. Lately I have been refactoring a lot of my code using functions. Being a noob however, I ran into some issues in that I didn’t understand how PHP scoped the usage of variables.
Case in point, I had written a function to generate a bunch of variables dynamically based on some input into the function, but I wasn’t able to use those variables outside of the function. (more…)
I have put it off for a long time… You know… actually really deep-diving and really learning a programming language. After reading, and then reading some more, and then talking to some developers where I work (we are a Microsoft Shop from pretty much top to bottom), and then reading a few more things… I have come down to learning in basically this progression (which probably sounds… and probably is… a bit haphazard):