[erlang-questions] Generating a list of lists containing random numbers

Pierre Fenoll pierrefenoll@REDACTED
Tue Jul 3 09:13:37 CEST 2018


How about this: (note: use whatever seeding you need)

rand:seed(exsplus, {1,2,3}),
AThousand = lists:seq(1, 1000),
[[rand:uniform() || _ <- AThousand]
 || _ <- AThousand
]

On Tue 3 Jul 2018 at 08:33, Raimo Niskanen <
raimo+erlang-questions@REDACTED> wrote:

> On Tue, Jul 03, 2018 at 01:15:17PM +0900, zxq9@REDACTED wrote:
> > On 2018年7月2日月曜日 23時36分10秒 JST Awsaf Rahman wrote:
> > > Hello!
> > >
> > > I am trying to determine the execution time for my matrix
> multiplication
> > > program and for that I need to generate a large enough List of Lists of
> > > random integers. Say I want to generate a matrix with a 1000 rows
> (that is
> > > 1000 lists inside a list). Is there any way of doing it? I can
> generate a
> > > list of random numbers using list comprehension but can't figure out
> how to
> > > go about building a list of lists.
> >
> > Wheels within wheels, man...
> >
> >   -module(genset).
> >   -export([generate/1]).
> >
> >   generate(Size) ->
> >       Generate = fun() -> rand:uniform(1000) end,
> >       MakeList = fun() -> populate(Generate, Size, []) end,
> >       populate(MakeList, Size, []).
> >
> >
> >   populate(_, Size, Acc) when Size < 0 ->
> >       Acc;
> >   populate(Generate, Size, Acc) ->
> >       populate(Generate, Size - 1, [Generate() | Acc]).
> >
> >
> > -Craig
>
> This may be a missing function for the lists module:
>
>     buildr(N, Fun, Acc) ->
>         buildr(N, Fun, Acc, []).
>     %%
>     buildr(0, _Fun, Acc, R) ->
>         {R, Acc};
>     buildr(N, Fun, Acc, R) ->
>         {V, NewAcc} = Fun(Acc),
>         buildr(N - 1, Fun, NewAcc, [V|R]).
>
> Example usage:
>
>     Base = 0,
>     Range = 1 bsl 32,
>     {ListOfLists, _NewSeed} =
>         buildr(
>             1000,
>             fun (Seed) ->
>                 buildr(
>                     1000,
>                     %%fun rand:uniform_s/1,
>                     fun (S) ->
>                         {V, NewS} = rand:uniform_s(Range, S),
>                         {Base + V - 1, NewS}
>                     end,
>                     Seed)
>             end,
>             rand:seed_s(exrop)),
>
> --
>
> / Raimo Niskanen, Erlang/OTP, Ericsson AB
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-- 

Cheers,
-- 
Pierre Fenoll
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180703/e8750222/attachment.htm>


More information about the erlang-questions mailing list