function [A,L]=buildrand(N,k)
%Builds a sparse, random network with N nodes & average degree k. No self-connections are allowed,
%network is undirected and k must be less than N.
%Usage: [A,L]=buildrand(N,k), where L is the total number of edges (returned as a check)
L=round(N*k/2);
nett = tril(rand(N),-1); %pulls the lower tri out of rand(N)
%sortnet=sort(nett(find(nett))); %what is faster?
sortnet = sort(reshape(nett,N^2,1)); %list random values as vector
B = sparse((nett > sortnet(end-L))); %take top L values
A = B + B'; %make symmetric: zeros(N) +