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

100 Days of Code – Day 27/28

My 100 Days of Code Repository * My FantasyGrounds XML to PDF converter Repo * My freeCodeCamp account page link * My CodePen.io Link

Day 27 saw me complete my Angular IO tutorial, and modify said tutorial to demo tabs for our Proof of Concept at work. I think next on this front is doing the same thing in ReactJS.

Day 28 I worked on more of the freeCodeCamp ‘Portfolio Page’ working out the footer section and adding all the said links I wanted to use. Next on this front will actually be handling the 3 sections (About, Portfolio, and Contact) and making it a little snazzy!

I also worked on a ‘special project’ that I won’t talk about unless it comes to fruition, but it’s basically seeing someone I respect have a need for my skills, and putting myself out there and offering to aid them in a unique-ish way (I hope).

Until tomorrow!

Never B Flat, Sometimes B Sharp, Always B Natural

100 Days of Code – Day 24

My 100 Days of Code Repository * My FantasyGrounds XML to PDF converter Repo * My freeCodeCamp account page link * My CodePen.io Link

Today was an odd day. Work decided that we wanted to update our system and redo our website – and we started working on a Proof of Concept with Angular and Typescript. Now, I had never used either of those, so I started to do some work learning AngularJS. So that’s what to day is – starting to learn Angular (as I’ve done more ‘after hours’ as well!) But since its on my work machine – it won’t be going up on my personal github account, etc.

Until tomorrow!

Never B Flat, Sometimes B Sharp, Always B Natural

100 Days of Code – Day 23

My 100 Days of Code Repository * My FantasyGrounds XML to PDF converter Repo * My freeCodeCamp account page link * My CodePen.io Link

Today I started my Personal Portfolio page for Challenge #120 for freeCodeCamp!

I spent a little time viewing some other portfolio projects to get a feel about what I want to do… figuring out which layouts I liked, and which ones I didn’t – to start to get the feel of how I wanted my page to flow. And I reviewed A LOT of the google fonts, and picked 3 that I enjoyed, and added them to the page…

There is lots more work to do here – I think this project will take a few days to get figured out! But hey – I’m learning as I go!

Until tomorrow!

Never B Flat, Sometimes B Sharp, Always B Natural

100 Days of Code – Day 22

My 100 Days of Code Repository * My FantasyGrounds XML to PDF converter Repo * My freeCodeCamp account page link * My CodePen.io Link

Today I finished by Queen Tribute page for Challenge #119 for freeCodeCamp!

I’m happy with it for not doing much design work (as its a basic assignment, I didn’t want to spend days going over layout and design, and then spend days coding it… it’s just an exercise to work on the html + css stuff we’ve worked on in fCC!)

Going to look at some issues with my PDF Maker project, maybe code some on that later tonight!

Until tomorrow!

Never B Flat, Sometimes B Sharp, Always B Natural

100 Days of Coding – Day 21

My 100 Days of Code Repository * My FantasyGrounds XML to PDF converter Repo * My freeCodeCamp account page link * https://codepen.io/MusicalCoder/pen/jwGYgg

21 Days Down… 79 More to go… With that said…

Today I’ve worked on more freeCodeCamp, starting Challenge 119, which is building a tribute page on codepen.io. I have this started, and mostly done. I’ll do a few more items on it, and then be ready to say i’ve completed it.

I’ve also completed issue #3 on my Fantasy Grounds XML to PDF converter. But also found another bug and logged it!

I plan on adding some additional exercises in for working on python work, mainly from CodeWars, CheckIO, CodeEval, and ProjectEuler… I’ve sort of become very disappointed with HackerRank, as a) they haven’t updated a lot of their python stuff from legacy python, and b) some of their puzzles aren’t explained well.

Until tomorrow…

Never B Flat, Sometimes B Sharp, Always B Natural