[erlang-questions] beginner question
Matt Williamson
<>
Sun Aug 17 18:27:40 CEST 2008
He's right. You are running the function once and passing the return value
for every iteration. Your modified program should look like this:
-module(scrap).
-compile(export_all).
set_seed_to_now() ->
{A1, A2, A3} = now(),
random:seed( A1, A2, A3 ).
for( Max, Max, F ) ->
[*F()*];
for( I, Max, F ) ->
[*F()*|for( I+1, Max, F )].
random_tuple( Max ) ->
{ random:uniform( Max ), random:uniform( Max ) }.
random_tuples( Max, Num ) ->
set_seed_to_now(),
for( 1, Num, *fun() -> random_tuple( Max ) end* ).
On Sat, Aug 16, 2008 at 11:37 PM, Håkan Stenholm <
> wrote:
> Ron Peterson wrote:
>
> > When I compile the code below and run it, I get something like the
> > following:
> >
> > 54> scrap:random_tuples( 20, 5 ).
> > [{3,9},{3,9},{3,9},{3,9},{3,9}]
> >
> > I'm sure this is expected behaviour. I'm just not yet familiar enough
> > with erlang to understand it. Why isn't this the same as calling
> > random_tuple a number of times in succession?
> >
> > TIA.
> >
> > set_seed_to_now() ->
> > {A1, A2, A3} = now(),
> > random:seed( A1, A2, A3 ).
> >
> > for( Max, Max, F ) ->
> > [F];
> > for( I, Max, F ) ->
> > [F|for( I+1, Max, F )].
> >
> > random_tuple( Max ) ->
> > { random:uniform( Max ), random:uniform( Max ) }.
> >
> > random_tuples( Max, Num ) ->
> > set_seed_to_now(),
> > for( 1, Num, random_tuple( Max ) ).
> you probably want to do something like:
>
> for( 1, Num, fun() -> random_tuple( Max ) end).
>
> and call F in for(...) as F() so that you pass a function to for(...)
> to run in each iteration, rather than simply passing a precalculated
> value as you do right now.
> _______________________________________________
> erlang-questions mailing list
>
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080817/0f55e958/attachment.html>
More information about the erlang-questions
mailing list