Generating a list of lists containing random numbers

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Generating a list of lists containing random numbers

Awsaf Rahman
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. 

Regards
Awsaf 

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Generating a list of lists containing random numbers

zxq9-2
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
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Generating a list of lists containing random numbers

Raimo Niskanen-2
On Tue, Jul 03, 2018 at 01:15:17PM +0900, [hidden email] 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Generating a list of lists containing random numbers

zxq9-2
On 2018年7月3日火曜日 8時33分12秒 JST Raimo Niskanen wrote:

> On Tue, Jul 03, 2018 at 01:15:17PM +0900, [hidden email] 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)),
>
>

I think I'd rather have matrix operations in a matrix module.

Every time I've needed to do matrix operations the need has either been:

1)
Trivial, flat matrix operations over regular matrices of regular
dimensions populated by scalar values & a well-defined _|_.

OR

2)
Matrices that are specialized enough that they really deserve a custom
set of handlers all their own.

In case 1 there are enough regular operations (especially against numeric
values) that you could fill out a full-blown matrix_math module and still
not cover everything. I wouldn't want that to clutter the lists module.

In case 2 trying to generalize is futile because remembering the details
of the abstractions involved is more trouble than writing a special handler,
hence things like this:
   https://gitlab.com/zxq9/zxWidgets/blob/master/src/zxw.erl#L194

I wouldn't want to attempt to generalize a text grid metawidget generator
because the assumptions embedded in it will break in any other situation,
but nonetheless, it is a matrix operation to generate, populate, and extract
or modify data in it.

I've got a naive matrix math module around here somewhere that I wrote as
an example for a guy on SO a while back... but it only handles a few math
operations, flips, inversions and rotations.

Would people be interested in a matrix_math library?

-Craig
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Generating a list of lists containing random numbers

Pierre Fenoll-2
In reply to this post by Raimo Niskanen-2
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 <[hidden email]> wrote:
On Tue, Jul 03, 2018 at 01:15:17PM +0900, [hidden email] 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
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
--

Cheers,
-- 
Pierre Fenoll


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Generating a list of lists containing random numbers

zxq9-2
In reply to this post by zxq9-2
On 2018年7月3日火曜日 16時08分18秒 JST [hidden email] wrote:

> On 2018年7月3日火曜日 8時33分12秒 JST Raimo Niskanen wrote:
> > On Tue, Jul 03, 2018 at 01:15:17PM +0900, [hidden email] 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...
> >
> > This may be a missing function for the lists module:
>
> I think I'd rather have matrix operations in a matrix module.
>
...
>
> Would people be interested in a matrix_math library?

I figured I could do a search and just find a native Erlang lib for this.

Sadly, all I found in native Erlang was:

1- My blog post about matrix rotation and multiplication I mentioned
earlier (evidently I posted it here https://zxq9.com/archives/1387 --
in all it's naive and assumptive glory)

2- A similar module that does a bit more but assumes a bit more as well
https://github.com/majelbstoat/Morgana/blob/master/src/matrix.erl

3- A module that looks like it was intended as an escript that sort of
freaks me out:
http://dada.perl.it/shootout/matrix.erlang.html

Not very comprehensive.

The more comprehensive solutions are bindings to C/C++ libs like BLAS
over CUDA, which are fine as long as you can build and run C/C++ code,
but NIFs are not easy to deal with for anyone not running on *nix:
https://github.com/vascokk/NumEr

Hm.

It seems like if there were a need for this that it would be covered
already. Is it too niche to worry with?

-Craig
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Generating a list of lists containing random numbers

Raimo Niskanen-2
In reply to this post by zxq9-2
On Tue, Jul 03, 2018 at 04:08:18PM +0900, [hidden email] wrote:
> On 2018年7月3日火曜日 8時33分12秒 JST Raimo Niskanen wrote:
: :

> > 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)),
> >
> >
>
> I think I'd rather have matrix operations in a matrix module.

This is not a matrix operation.

We have lists:seq/2,3 and lists:duplicate/2 but no such generating function
that takes a generator fun.

It could be named generate/3, gen/3 or build/3.  I suggested buildr/3
because it builds from the right end of the list analogous to foldr/3.

A buildl/3 would be more inefficient since it should return {List,Acc}
which does not allow for a recursive variant so it would become build3/r
followed by reverse/1.

So:     lists:buildr(N, GeneratorFun, Acc) -> {List, NewAcc}
with:       GeneratorFun(Acc) -> {Value, NewAcc}

Might be useful enough to add...


>
> Every time I've needed to do matrix operations the need has either been:
>
> 1)
> Trivial, flat matrix operations over regular matrices of regular
> dimensions populated by scalar values & a well-defined _|_.
>
> OR
>
> 2)
> Matrices that are specialized enough that they really deserve a custom
> set of handlers all their own.

In Wings3D I wrote a matrix library that was optimized for sparse matrixes,
for the automatic U-V mapping algorithm.

Yet another specialized matrix library I guess...

/ Raimo


>
> In case 1 there are enough regular operations (especially against numeric
> values) that you could fill out a full-blown matrix_math module and still
> not cover everything. I wouldn't want that to clutter the lists module.
>
> In case 2 trying to generalize is futile because remembering the details
> of the abstractions involved is more trouble than writing a special handler,
> hence things like this:
>    https://gitlab.com/zxq9/zxWidgets/blob/master/src/zxw.erl#L194
>
> I wouldn't want to attempt to generalize a text grid metawidget generator
> because the assumptions embedded in it will break in any other situation,
> but nonetheless, it is a matrix operation to generate, populate, and extract
> or modify data in it.
>
> I've got a naive matrix math module around here somewhere that I wrote as
> an example for a guy on SO a while back... but it only handles a few math
> operations, flips, inversions and rotations.
>
> Would people be interested in a matrix_math library?
>
> -Craig
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

--

/ Raimo Niskanen, Erlang/OTP, Ericsson AB
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Generating a list of lists containing random numbers

empro2
In reply to this post by Awsaf Rahman
Are lists not collections of varying size, that tend to grow
or shrink? (there is a tuple_size/1 but no lists:size/1)
and to contain things of not necessarily the same type? (e.
g. proplists)?

Are vectors and matrices not of rather fixed dimensions and
tend to contain similar things (e. g. R^3, or in this thread
R^1000, or Z^1000, or N^1000)?

When I think "matrix", things that come to mind are:

        * {...} (tuple) and element(3, element(2, M)) and
          setelement/3, ...,

        * stdlib's array:get(3, array:get(2, A)),

        * and in this case especially: array:map(Fun, A).

But these are just some stranger's thoughts ...

Michael

--

That, which was said, is not that, which was spoken,
but that, which was understood; and none of these
necessarily comes close to that, which was meant.












_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Generating a list of lists containing random numbers

Hugo Mills-2
On Wed, Jul 04, 2018 at 02:03:42PM +0200, [hidden email] wrote:

> Are lists not collections of varying size, that tend to grow
> or shrink? (there is a tuple_size/1 but no lists:size/1)
> and to contain things of not necessarily the same type? (e.
> g. proplists)?
>
> Are vectors and matrices not of rather fixed dimensions and
> tend to contain similar things (e. g. R^3, or in this thread
> R^1000, or Z^1000, or N^1000)?
>
> When I think "matrix", things that come to mind are:
>
> * {...} (tuple) and element(3, element(2, M)) and
>  setelement/3, ...,
>
> * stdlib's array:get(3, array:get(2, A)),
>
> * and in this case especially: array:map(Fun, A).
>
> But these are just some stranger's thoughts ...
   Most numerics libraries use single-dimensional lists for storage,
and turn the (i, j) locations of the matrix into (i*width+j) to look
it up in the list, because it's generally more efficient to do so with
random-access data.

   If you look at the numerics libraries, the general assumption is
that you have linear random-access storage of the data, and then a
thin translation layer on top of that which converts (i, j)
coordinates to the linear location.

   Note also that there are generalisations to matrices which have
more than two indices (and are hence written as n*m*p*q*... objects),
which are used in some things like general relativity. These are
handled in exactly the same way, with a slightly different, but
generalisable, translation function.

   There are definitely algorithms which require taking slices out of
matrices, for example, building the (n-1)*(n-1) matrix consisting of
every row that isn't row i, and every column that isn't column j of
some n*n matrix. Similarly, there are some algorithms which use block
decompositions of matrices (say, the four n/2*n/2 square matrices
making up the whole n*n). Both of these can be done quite easily by,
again, generating specific translation functions to map to the
underlying linear matrix.

   So, as long as you can do a linear random-access storage unit
(probably what erlang's array module was designed for in the first
place), you can build all fo the useful things on top of that with
appropriate [stacks of] coordinate translation functions.

  Hugo.

--
Hugo Mills             | Stick them with the pointy end.
hugo@... carfax.org.uk |
http://carfax.org.uk/  |
PGP: E2AB1DE4          |                                              Jon Snow

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

signature.asc (853 bytes) Download Attachment