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.
<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() -><br>
{A1, A2, A3} = now(),<br> random:seed( A1, A2, A3 ).<br><br>for( Max, Max, F ) -><br> [F()];<br>for( I, Max, F ) -><br> [F()|for( I+1, Max, F )].<br><br>random_tuple( Max ) -><br>
{ random:uniform( Max ), random:uniform( Max ) }.<br><br>random_tuples( Max, Num ) -><br> set_seed_to_now(),<br> for( 1, Num, fun() -> random_tuple( Max ) end ).<br><br><div class="gmail_quote">
On Sat, Aug 16, 2008 at 11:37 PM, Håkan Stenholm <span dir="ltr"><<a href="mailto:hokan.stenholm <at> bredband.net">hokan.stenholm <at> bredband.net</a>></span> wrote:<br><blockquote class="gmail_quote">
<div class="Ih2E3d">Ron Peterson wrote:<br><br>
> When I compile the code below and run it, I get something like the<br>
> following:<br>
><br>
> 54> scrap:random_tuples( 20, 5 ).<br>
> [{3,9},{3,9},{3,9},{3,9},{3,9}]<br>
><br>
> I'm sure this is expected behaviour. I'm just not yet familiar enough<br>
> with erlang to understand it. Why isn't this the same as calling<br>
> random_tuple a number of times in succession?<br>
><br>
> TIA.<br>
><br>
> set_seed_to_now() -><br>
> {A1, A2, A3} = now(),<br>
> random:seed( A1, A2, A3 ).<br>
><br>
> for( Max, Max, F ) -><br>
> [F];<br>
> for( I, Max, F ) -><br>
> [F|for( I+1, Max, F )].<br>
><br>
> random_tuple( Max ) -><br>
> { random:uniform( Max ), random:uniform( Max ) }.<br>
><br>
> random_tuples( Max, Num ) -><br>
> set_seed_to_now(),<br>
> for( 1, Num, random_tuple( Max ) ).<br>
</div>you probably want to do something like:<br><br>
for( 1, Num, fun() -> 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>