Skip to content

Instantly share code, notes, and snippets.

@samrat
samrat / randomWalk.elm
Last active August 29, 2015 14:03
Random walk in Elm
import Random
(w,h) = (600, 400)
initialPosition = (0, 0)
intToDir d n =
if d < 0.5
then n
else -n
@samrat
samrat / randomLeaps.elm
Last active August 29, 2015 14:03
Random walk with "leaps"
import Random
(w,h) = (600, 400)
initialPosition = (0, 0)
step2 seed =
let floats = Random.floatList (always 4 <~ seed)
randCoords [f1,f2,f3,f4] =
let s = if f1 < 0.01 then 50 else 1
n = if f2 < 0.5 then s else -s
@samrat
samrat / notPerlin.elm
Created July 5, 2014 16:06
Failed attempt at Perlin- not random enough!
import Generator as Gen
import Generator.Standard as Gs
(w,h) = (100,100)
-- Vectors
type Vec2 = { x : Float
, y : Float
}
@samrat
samrat / ieee.erl
Created September 7, 2014 10:01
Convert a binary representation to IEEE floats
%% K - number of bits to represent `exp`,
%% N - number of bits to represent `frac`.
%% | s | exp(k bits) | frac(n bits) |
make_float_converter(K, N) ->
Bias = math:pow(2, K-1) - 1,
Max_denom = math:pow(2, N),
Max_exp = round(math:pow(2, K) - 1),
fun(<<S:1, Exp:K, Frac:N>>) ->
@samrat
samrat / square_lu.c
Last active August 29, 2015 14:11
LU factorization of square matrices. This is an implementation of the algorithm given in Strang's Intro to Linear Algebra textbook
#include <stdio.h>
#include <math.h>
void square_lu(float A[][3], float L[][3], float U[][3])
{
int n = 3;
for (int k = 0; k < n; k++)
{
L[k][k] = 1.0f;
for (int i = k+1; i < n; i++)
@samrat
samrat / Pascal.jl
Created December 22, 2014 15:53
Create a lower triangular n × n matrix filled with the first n rows of Pascal’s triangle.
module Pascal
export pascal
function factorial(n)
if n==0; return(1); end
result = n * factorial(n-1)
return(result)
end
@samrat
samrat / tridiag.jl
Created February 4, 2015 14:43
Tridiagonal matrix
function tridiag_elem(i,j)
if abs(j-i) > 1; return(0); end
return(1);
end
function tridiag_mat(n)
[tridiag_elem(i,j) for i in [0:n-1], j in [0:n-1]]
end
@samrat
samrat / hashtable.rkt
Created March 6, 2015 09:45
Quick n' dirty hashtable implementation in Racket
#lang racket
;; USAGE:
;; =====
;; (init-hashtable 100)
;; (insert ht 13 3)
;; (lookup ht 13)
;; (delete ht 13)
;; list of random integers in [0, num-buckets); used for hashing
@samrat
samrat / run_bench.ml
Created July 22, 2015 07:40
run_bench.ml
> #require "core_bench"
> open Core.Std;;
> open Core_bench.Std;;
let run_bench tests =
Command.run (Bench.make_command tests);;
open Core.Std
type point = {x : float;
y : float}
let avg_two a b =
0.75 *. a +. 0.25 *. b
let avg_two_points a b =
{x= (avg_two a.x b.x);