Created
February 15, 2014 04:02
-
-
Save vibhavp/9014378 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun make-random-bitarray (n) | |
"Generate a random list of bits of length N" | |
(let ((arr nil) | |
(i 0)) | |
(loop while (not (eq i n)) do | |
(progn (incf i 1) (push (random 2) arr))) | |
arr)) | |
(defun make-random-adj-mat (n) | |
"Generate a random adjacency matrix (for a digraph) of size n" | |
(let ((mat nil) | |
(i 0)) | |
(loop while (not (eq i n)) do | |
(progn (incf i 1) (push (make-random-bitarray n) mat))) | |
mat)) | |
;; Generate graphs using (make-dot-from-mat (make-random-bitarray <size>) <filename>) | |
(defun make-dot-from-mat (mat filename) | |
(let ((i 0) | |
(j 0) | |
(out (open filename :direction :output :if-does-not-exist :create))) | |
(write-line "digraph {" out) | |
(dolist (arr mat) | |
(dolist (bit arr) | |
(unless (eq 0 bit) | |
(write-line (format nil "~a -> ~a [];" i j) out)) | |
(incf j 1)) | |
(incf i 1) | |
(setq j 0)) | |
(write-line "}" out) | |
(close out))) |
sjatkins
commented
Sep 14, 2014
- A random bit array of size N is a random number up to 2**n converted to bits.
- Use the dotimes macro .
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment