vrijdag 28 augustus 2015

Randomness....! Is random the same as random?

Often I need a random number (as a seed for a split, or to randomly order records). Now you might think that,  if we have a randomnumber generator, you could make things MORE random if you multiply these (random * random is randomer). Let's see if this holds true by just trying!
Say we have 20,000 random numbers between 0 and 1 we can plot the number against their frequency using a histogram (100 bins):

Now this looks pretty random, 100 bins means that the space is divided in 100 sections. So 1 bin (section) with all values between 0.00 - 0.01 , 1 bin with all values between 0.01-0.02.

We observe that most bins have a value between 0.009 & 0.011 and all between 0.008 & 0.012. which means that there are about 0.010 * 20,000 = +/- 200 points generated with a value between 0 and 0.01.

There are also 200 point generated with a value between 0.01 and 0.02 and so on. This indeed looks rather random.  We can also plot the index (1st number generated to 20,000th number generated) versus the value:

Again this looks al pretty random, and so it should. The whole space is filled evenly.

Now here comes the kicker: If we multiply these random numbers (the same in the same order) with another random number things start to look less random:

Again we can plot the index versus the number:

What we see is that the numbers are actually getting less random. So a given value occurs more frequently. Now let's look at the bin plot again:

Remember just now, there were approximately 200 points with a value between 0.00 and 0.01. Now we see 0.06 * 20,000 = 1200, conversely we see only 0.00005 * 20,000 = 1 datapoint with a value between 0.99 and 1.00! This is looking much less random.

And this effects gets worse when we use 3 times random numbers multiplied:

0.165 * 20,000 = 3310 values between 0.00 - 0.01  and 0 values between 0.99 - 1.00. The highest bin is between 0.89 and 0.90 with 0.0001 * 20,000 = 2 data points.

And 4 times random numbers multiplied…

0.33030 * 20,000 = 6606 values between 0.00 and 0.01 & again 0 in the highest bin. There is 0.00005 * 20,000 = 1 datapoint with a value between 0.86 and 0.87. (perhaps not very visible in the second plot)

So there you have it, while you might think that random * random is more random than random, it is NOT! This is stated by the central limit theorem. In probability theory, the central limit theorem (CLT) states that, given certain conditions, the arithmetic mean of a sufficiently large number of iterates of independent random variables, each with a well-defined expected value and well-defined variance (all will be between 0 and 1), will be approximately normally distributed, regardless of the underlying distribution. (see [1] http://www.math.uah.edu/stat/sample/CLT.html, [2] Rice, John (1995), Mathematical Statistics and Data Analysis (Second ed.), Duxbury Press, ISBN 0-534-20934-3), also [3] wikipedia, https://en.wikipedia.org/wiki/Central_limit_theorem )

dinsdag 4 augustus 2015

Fix WIFI issues in OS X Yosemite

OS X (in particular Yosemite) is not known for it's reliable wifi connections. There are a number of solutions posted over the internet (see below) which I have not tried. However, if you, like me, have wifi issues after installing a recent os x update this might be a solution. The problem is that the update renames (backups) the discoveryd.plist file without installing a new one. Simple workaround (maintaing the backup and renaming the _bc to original):
- cd /System/Library/LaunchDaemons/
- sudo cp com.apple.discoveryd_bc.plist com.apple.discoveryd_bc2.plist
- sudo mv com.apple.discoveryd_bc.plist com.apple.discoveryd.plist
- sudo reboot

Here are some mroe fixes from OS X Daily (have not tried / needed those myself).
Original article can be found here : http://osxdaily.com/2014/10/25/fix-wi-fi-problems-os-x-yosemite/

1: Remove Network Configuration & Preference Files

Manually trashing the network plist files should be your first line of troubleshooting. This is one of those tricks that consistently resolves even the most stubborn wireless problems on Macs of nearly any OS X version. This is particularly effective for Macs who updated to Yosemite that may have a corrupt or dysfunctional preference file mucking things up:
1. Turn Off Wi-Fi from the Wireless menu item
2. From the OS X Finder, hit Command+Shift+G and enter the following path:
3. `/Library/Preferences/SystemConfiguration/`
4. Within this folder locate and select the following files:
5. `com.apple.airport.preferences.plist com.apple.network.identification.plistcom.apple.wifi.message-tracer.plist NetworkInterfaces.plist preferences.plist`
6. Move all of these files into a folder on your Desktop called ‘wifi backups’ or something similar – we’re backing these up just in case you break something but if you regularly backup your Mac you can just delete the files instead since you could restore from Time Machine if need be
7. Reboot the Mac
8. Turn ON WI-Fi from the wireless network menu again
This forces OS X to recreate all network configuration files. This alone may resolve your problems, but if you’re continuing to have trouble we recommend following through with the second step which means using some custom network settings.

2: Create a New Wi-Fi Network Location with Custom DNS

What we’re doing here is creating a new network location which is going to have a configuration different from the defaults. First, we’ll use a completely new network setup. Then, we’ll set DNS on the computer rather than waiting for OS X to get DNS details from the wi-fi router, which alone can resolve many issues with DNS lookups, since Yosemite seems to be finicky with some routers. Finally, we’re going to set a custom MTU size that is slightly smaller than the default, which will get rejected less often by a router, it’s an old netadmin trick that has long been used to fix network troubles.
1. Open the  Apple menu and go to System Preferences, then choose “Network”
2. Pull down the “Locations” menu and choose “Edit Locations”, then click the [+] plus button, give the new network location a name like “Yosemite WiFi” then click Done
3. Next to “Network Name” join your desired wifi network as usual
4. Now click the “Advanced” button, and go to the “DNS” tab
5. Click the [+] plus button and specify a DNS server – we’re using 8.8.8.8 for Google DNS in this example but you should use the fastest DNS servers you can find for your location, it will vary. You can also use your own ISP DNS servers
6. Now go to the “Hardware” tab and click on ‘Configure’ and choose “Manually”
7. Click on MTU and change it to “Custom” and set the MTU number to 1453 (this is a networking secret from ancient times, and yes it still works!), then click on “OK”
8. Now click on “Apply” to set your network changes
Quit and relaunch any apps that require network access, like Safari, Chrome, Messages, Mail, and your wireless connectivity should be flawless and back at full speed at this point.

Reset SMC

Some users report that resetting the System Management Controller is sufficient to stir their Wi-Fi back into action. Since many users have a MacBook laptop, that’s what we’ll cover first:
• Turn off the MacBook Air or MacBook Pro
• Connect the power adapter to the Mac as usual
• On the keyboard, press and hold down the Shift+Control+Option keys and the Power button at the same time, hold them all for a few seconds
• Release all keys and the power button at the same time by lifting your hands away from the keyboard
• Boot the Mac as usual
You can read more about resetting SMC here and here for other Macs, including for the iMac and Mac Mini.

Unload & Reload discoveryd to Fix DNS & Wi-Fi Failures in OS X Yosemite

Another trick that was left in the comments (thanks Frank!) involves refreshing the discoveryd service by unloading and reloading it with the launchctl command. This is a bit curious but apparently it works for some users, suggesting there could be an issue with discovery or resolving DNS on some Yosemite Macs. It’s certainly worth a try if the above tricks failed to resolve your wi-fi connectivity problems in OS X 10.10, as there are a fair amount of positive reports with this one:
1. Open Terminal (found in /Applications/Utilities/ or with Spotlight) and enter the following command:
2. `sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist`
3. Hit return and enter an admin password to use the sudo command
4. Now run the following command to reload discoveryd (this used to be called mDNSResponder)
5. `sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist`
You may need to relaunch apps that require network connectivity. Note that if you reboot the Mac with this one, you will have to repeat the above steps to unload and reload discoveryd into launchd.

Bonus OS X Yosemite Wi-Fi Troubleshooting Tricks

Here are some other less than ideal solutions that have been reported to remedy wi-fi issues in OS X Yosemite.
• Join a 2.4GHZ network (N network) – some users report no trouble with 2.4GHz networks
• Set the wi-fi routers 5GHz (G) channel to be somewhere between 50-120
• Turn Off Bluetooth – We have seen several reports that disabling Bluetooth will resolve wifi problems with some networks, but this is obviously not appropriate for Macs that have bluetooth accessories
If none of the above works, there could be other problems. Sometimes starting fresh with a clean install could resolve them, or if you believe the problem to be a bug and you had a trouble free experience in prior versions of Mac OS, you could always downgrade from OS X Yosemite to Mavericks again until an update to Yosemite arrives to resolve the issue once and for all.
Have you experienced wireless connectivity issues with OS X Yosemite? What have you tried, and how did you resolve them? Let us know what has been working to remedy your wifi troubles by leaving a comment!