Find the frequency of random numbers generated by rand-int in Clojure


TLDR:

We will write a short program in Clojure that calls rand-int repeatedly 
and print the frequencies of each numbers that was generated.

Solution:

(frequencies (take 1000 (repeatedly #(rand-int 10))))
{0 85, 7 116, 1 108, 4 108, 6 97, 3 98, 2 95, 9 101, 5 87, 8 105}

 

rand-int function takes one argument, and it returns a random number between 0 and argument.


=> (rand-int 10)

2

We will call rand-int using repeatedly function, but repeatedly only accepts function with no argument. So we will place the call to rand-int inside a anonymous function that does not take any argument.


(repeatedly (fn [] (rand-int 10)))

Since repeatedly returns infinite sequence, we will take the first 10 output,


=> (take 10 (repeatedly (fn [] (rand-int 10))))
(2 2 5 3 5 4 1 6 2 2)

To find our the frequency of each a number generated, we can use frequencies function.


=> (frequencies (take 10 (repeatedly (fn [] (rand-int 10)))))
{5 1, 9 1, 0 2, 3 1, 8 2, 7 2, 6 1}

We can see that 5, 9, 3 and 6 was generated once, 7, 8 and 0 were generated twice.

Lets increase the sample size to 100 and 1000,


=> (frequencies (take 100 (repeatedly (fn [] (rand-int 10)))))
{0 8, 7 8, 1 10, 4 16, 6 8, 3 10, 2 14, 9 6, 5 9, 8 11}
=> (frequencies (take 1000 (repeatedly (fn [] (rand-int 10)))))
{0 85, 7 116, 1 108, 4 108, 6 97, 3 98, 2 95, 9 101, 5 87, 8 105}

Anonymous function can be written in a more simplified format. We can rewrite fn [] (rand-int 10) as #(rand-int 10).

So our final code looks like,


=> (frequencies (take 1000 (repeatedly #(rand-int 10))))
{0 110, 7 93, 1 95, 4 92, 6 92, 3 107, 2 98, 9 108, 5 105, 8 100}

Installing exec plugin in Leiningen project

We can install the plugin globally, or per-project.

In both cases we will add the following line to two different files.


:plugins [[lein-exec "0.3.6"]]

To install globally, find the profiles.clj under C:\Users\<user name>\.lein in Windows, and /home/<user name>/.lein in Linux.

After adding the plugin information our profiles.clj will look like following,


{:user
{
:java-cmd "C:\\Java\\jdk1.8.0_131\\bin\\java.exe"
:plugins [ [lein-exec "0.3.6"] ]
}
}

To install the plugin per project find the project.clj at the root directory of the project. After adding the plugin information the file will look like the following,


(defproject scratch "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]]
:plugins [[lein-exec "0.3.6"]])

Now run lein deps in both cases to download the plugin from clojars.org and add it to the local repository.

(Note : Before adding the plugin statement in both profiles.clj and project.clj represent a clean installation and basic project generated by Leiningen.)

To compile and run a Clojure file, use the following command


lein exec .\src\ProjectName\core.clj

Also run lein help exec to see the help file associated with the plugin.

How to change the prompt color in Windows Powershell

Open Powershell and run $profile


PS C:\Users\irfan> $profile
C:\Users\<user>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

This file is similar to bashrc file in for the bash shell.

If you try to navigate to that location you would find the WindowsPowerShell folder does not exists.

Go to, C:\Users\<user>\Documents and create WindowsPowerShell folder and Microsoft.PowerShell_profile.ps1 file inside it.

Enter the following code in Microsoft.PowerShell_profile.ps1, Source


function Prompt
{
&amp;nbsp; &amp;nbsp; $promptString = "PS " + $(Get-Location) + "&amp;gt;"
&amp;nbsp; &amp;nbsp; Write-Host $promptString -NoNewline -ForegroundColor Yellow
&amp;nbsp; &amp;nbsp; return " "
}

Now if you run PowerShell Windows would try to use the profile file. But it wont be not because of security restriction.

Run the following command on Powershell , Details Here

Set-ExecutionPolicy RemoteSigned

Choose Yes.

Restart Powershell, and now the prompts will always show up in Yellow.

Untitled

How to find SuperCollider server port number?

SuperCollider Server usually starts at port 57110, but here we will double check if it’s actually running on that port.

When yo run SuperCollider IDE you are presented with the following screen,

Untitled

To start the server (from the menu bar) goto Language > Boot Server.

In the Post Window, you will see SuperCollider log. You can find the port number from the log.

Untitled

You can also type s.addr; and press Shift+Enter, and the port number with be printed in the Port Windows,

Untitled

IP and port number printed,

Untitled

Since you are running the server on your local machine, the IP address is 127.0.0.1