TIL: Algorithm for Random Numbers that sum to a value.

So I was on Reddit today, and someone had posted in the learning python subreddit about his random number function was running slow…

Specifically, he was creating a list of 9 random numbers between 0 and 100 (inclusive) where the sum of those 9 numbers totaled 100. I’ve never had a situation that called for that before, and the code he gave was a simple while statement saying that while the sum of the list didn’t equal 100, empty the list, and generate 9 numbers between 0 and 100 and add them to the list, and keep going until you got a total of 100…

So I commented that of course that would take ages, as the loop probably had to run tens of thousands of times to randomly get 9 different numbers between 0 and 100 whose sum totaled 100.

My original suggestion had been to get your first number, then generate a second number between 0 and 100 – first number, and then keep doing that…

When someone else posted the following quite elegant solution… I asked where he learned it from, and he couldn’t remember where, but after further investigating, its one of those quaint mathematical formulae that always works!!!

So, in our example, we need 9 numbers between 0 and 100 (inclusive), so we take 0, and 100, and then 8 random numbers between 0 and 100, and then sort them all, so we end up with 0, 8 numbers from smallest to largest, 100…

Then you simply take the difference between each set…

So for example, let’s say our 8 random numbers were 85, 24, 58, 5, 31, 46, 94, and 7. We then take those 8 numbers, add 0 and 100 as well, and then sort them, giving us:
[0, 5, 7, 24, 31, 46, 58, 85, 94, 100]

We then simply calculate the difference between each number (5-0, 7-5, 24-7, 31-24, etc) which, by doing so would give us:
[5, 2, 17, 7, 15, 12, 27, 9, 6]

Which, in turn, sum up to 100!!

I was like… HOLY CRAP!!! I’m keeping that one in my back pocket!!!

Never B Flat, Sometimes B Sharp, Always B Natural

New Storage/Work Drive

So I have a new computer that I never brought up (I’ve had it about 6 months, but again, never posted the new specs here – I’ll be doing that soon as well!) and I’ve got 2 SSDs in it (smallish for me – 240GB each) which one is root, and the other is home… But I’ve recently installed a 3TB 7200RPM Hard drive to use for storage and work space (I’ll keep downloads and other files on it so as not to waste space on the SSD)…

I wanted to write down how I did it for future reference, and to let anyone else know how it was done!

So first, we format the drive! I opened my terminal and did the following:
lsblk – which showed me I have a sda1/2 (swap and root), sdb1 (home) and sdc with nothing (unmounted).

So from there, I did the following:

sudo parted -s -a optimal /dev/sdc mklabel gpt
sudo parted -s -a optimal /dev/sdc mkpart primary 0% 100%
sudo mkfs.xfs /dev/sdc1

The first line configured my new hard drive to use GPT (since it’s 3TB, and msdos label won’t work).
The second line then made the partition as a primary partition, and then used the full amount of the hard drive.
The third link then formatted it as an XFS file system.

The next thing we’ll want to do is set it so that it auto mounts up on booting up (just like root and home do) so to do that, we’ll need to modify fstab – but before doing that, we’ll want to use the uuid of the hard drive – so that the system will ALWAYS mount just this hard drive to the specific location (which I should probably create as well).


sudo blkid
sudo mkdir /mnt/storage
sudo vim /etc/fstab

And then I simply added
UUID=[blkid] /mnt/storage xfs defaults 0 0
to my fstab file.

After rebooting, I now had my storage drive… Then I just need to do the following:

sudo mkdir /mnt/data/musicalcoder
sudo chown musicalcoder:musicalcoder /mnt/data/musicalcoder
sudo chmod 700 /mnt/data/musicalcoder

Then I can simply copy which folders I want from my home directory to my new storage drive, and then create a symbolic link to the new location in my home directory… I’ll use Downloads as an example…

mv -v ~/Downloads /mnt/data/musicalcoder/
ln -s /mnt/data/musicalcoder/Downloads ~/Downloads

Simply do that for each of the folders I wish to have on the storage drive (which for me were Downloads, Music, Videos, and Work), and voila!!!

Never B Flat, Sometimes B Sharp, Always B Natural

Back – and better than ever!

So I sort of stopped my ‘summer of code’ and 100 Days of coding, as I was having problems staying into it, and I realized that I wasn’t enjoying it, as I was working on things I wasn’t interested in and felt like I was forcing myself into it… so I stepped back…

Things are slightly tumultuous at work, so I decided it was time to write again, on stuff I wanted to write about, and on projects I want to work on!

I’m going to try to keep each post separate and worth reading on a single topic… so I’m going to sign off on this one for now!

More Soon!

Never B Flat, Sometimes B Sharp, Always B Natural