Ron Peterson | 17 Aug 05:03

beginner question

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 ) ).

--

-- 
Ron Peterson
(Continue reading)

Håkan Stenholm | 17 Aug 05:37

Re: beginner question

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:
(Continue reading)

Matt Williamson | 17 Aug 18:27
Gravatar

Re: beginner question

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 <hokan.stenholm <at> bredband.net> 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
erlang-questions <at> erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions

<div><div dir="ltr">He's right. You are running the function once and passing the return value for every iteration. Your modified program should look like this:<br><br>-module(scrap).<br>-compile(export_all).<br><br>set_seed_to_now() -&gt;<br>
&nbsp;&nbsp; {A1, A2, A3} = now(),<br>&nbsp;&nbsp; random:seed( A1, A2, A3 ).<br><br>for( Max, Max, F ) -&gt;<br>&nbsp;&nbsp; [F()];<br>for( I, Max, F ) -&gt;<br>&nbsp;&nbsp; [F()|for( I+1, Max, F )].<br><br>random_tuple( Max ) -&gt;<br>
&nbsp;&nbsp; { random:uniform( Max ), random:uniform( Max ) }.<br><br>random_tuples( Max, Num ) -&gt;<br>&nbsp;&nbsp; set_seed_to_now(),<br>&nbsp;&nbsp; for( 1, Num, fun() -&gt; random_tuple( Max ) end ).<br><br><div class="gmail_quote">
On Sat, Aug 16, 2008 at 11:37 PM, H&aring;kan Stenholm <span dir="ltr">&lt;<a href="mailto:hokan.stenholm <at> bredband.net">hokan.stenholm <at> bredband.net</a>&gt;</span> wrote:<br><blockquote class="gmail_quote">
<div class="Ih2E3d">Ron Peterson wrote:<br><br>
&gt; When I compile the code below and run it, I get something like the<br>
&gt; following:<br>
&gt;<br>
&gt; 54&gt; scrap:random_tuples( 20, 5 ).<br>
&gt; [{3,9},{3,9},{3,9},{3,9},{3,9}]<br>
&gt;<br>
&gt; I'm sure this is expected behaviour. &nbsp;I'm just not yet familiar enough<br>
&gt; with erlang to understand it. &nbsp;Why isn't this the same as calling<br>
&gt; random_tuple a number of times in succession?<br>
&gt;<br>
&gt; TIA.<br>
&gt;<br>
&gt; set_seed_to_now() -&gt;<br>
&gt; &nbsp; &nbsp; {A1, A2, A3} = now(),<br>
&gt; &nbsp; &nbsp; random:seed( A1, A2, A3 ).<br>
&gt;<br>
&gt; for( Max, Max, F ) -&gt;<br>
&gt; &nbsp; &nbsp; [F];<br>
&gt; for( I, Max, F ) -&gt;<br>
&gt; &nbsp; &nbsp; [F|for( I+1, Max, F )].<br>
&gt;<br>
&gt; random_tuple( Max ) -&gt;<br>
&gt; &nbsp; &nbsp; { random:uniform( Max ), random:uniform( Max ) }.<br>
&gt;<br>
&gt; random_tuples( Max, Num ) -&gt;<br>
&gt; &nbsp; &nbsp; set_seed_to_now(),<br>
&gt; &nbsp; &nbsp; for( 1, Num, random_tuple( Max ) ).<br>
</div>you probably want to do something like:<br><br>
for( 1, Num, fun() -&gt; random_tuple( Max ) end).<br><br>
and call F in for(...) as F() so that you pass a function to for(...)<br>
to run in each iteration, rather than simply passing a precalculated<br>
value as you do right now.<br><div>
<div></div>
<div class="Wj3C7c">_______________________________________________<br>
erlang-questions mailing list<br><a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</div>
</div>
</blockquote>
</div>
<br>
</div></div>
Richard A. O'Keefe | 18 Aug 02:50
Favicon

Re: beginner question


On 17 Aug 2008, at 3:07 pm, Ron Peterson wrote:
> for( Max, Max, F ) ->
>    [F];
> for( I, Max, F ) ->
>    [F|for( I+1, Max, F )].

There are two issues here.
First, for(1, 0, F) will crash instead of returning an empty list.
Why?

Second, you are making a list all of whose elements are the
*value* F.  With a name like that, you almost certainly wanted

for(LB, UB, Fun) ->
     [Fun() || I <- lists:seq(LB, UB)].

or even

for(LB, UB, Fun) ->
     [Fun(I) || I <- lists:seq(LB, UB)].

Better still, ditch for/3 entirely, and write
the list comprehension in place.
>
>
> random_tuple( Max ) ->
>    { random:uniform( Max ), random:uniform( Max ) }.
>
> random_tuples( Max, Num ) ->
>    set_seed_to_now(),
>    for( 1, Num, random_tuple( Max ) ).

This should be

random_tuples(Max, Count) ->
     set_seed_to_now(),
     [random_tuple(Max) || I <- lists:seq(1, Count)].

--
If stupidity were a crime, who'd 'scape hanging?

Ron Peterson | 25 Aug 04:33

Re: beginner question

2008-08-17_20:50:24-0400 "Richard A. O'Keefe" <ok <at> cs.otago.ac.nz>:

> or even
> 
> for(LB, UB, Fun) ->
>     [Fun(I) || I <- lists:seq(LB, UB)].
> 
> Better still, ditch for/3 entirely, and write
> the list comprehension in place.

(Getting back to this after a few days...)

Got it, e.g.

random_tuples( Max, Num ) ->
    set_seed_to_now(),
    [ random_tuple( Max ) || _ <- lists:seq( 1, Num ) ].

I was playing w/ for/3 mostly as an intellectual excercise.  I'm really
very new w/ this.  Thanks for the help.

--

-- 
Ron Peterson
Network & Systems Manager
Mount Holyoke College
http://www.mtholyoke.edu/~rpeterso
-
I wish my computer would do what I want it to do - not what I tell it to do.

Gmane