MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

random inside module

  • To: mathgroup at smc.vnet.net
  • Subject: [mg67692] random inside module
  • From: "Arkadiusz Majka" <Arkadiusz.Majka at telekomunikacja.pl>
  • Date: Wed, 5 Jul 2006 04:17:05 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

DearAll,

The code below works fine

ClearAll[list, maja, a]
maja[T_] := Module[{i, p = 2}, list[0_] = {1, 2, 3};
    a[i_] := 0 /; Mod[i, p] != 0;
    a[i_] := Random[Integer, {1, 10}] /; Mod[i, p] == 0;
    list[i_] := list[i] = Prepend[list[i - 1], a[i]]; list[T]]

and may have an output

Table[maja[i],{i,...}]

{{1,2,3},{0,1,2,3},{9,0,1,2,3},{0,9,0,1,2,3},{7,0,9,0,1,2,3},...}

we see that maja[i] differs from maja[i-1] that one element is added in
front of the list (0 or Random(1,10) )

Now I want to manipulate p , so I create Module

ClearAll[list, maja, a]
maja[T_, p_] := Module[{i, list, a}, list[0_] = {1, 2, 3};
    a[i_] := 0 /; Mod[i, p] != 0;
    a[i_] := Random[Integer, {1, 10}] /; Mod[i, p] == 0;
    list[i_] := list[i] = Prepend[list[i - 1], a[i]]; list[T]]


The problem is now that, because I had to put 'list' within {}, the
program is generating always new random numbers and I get

{1,2,3},{0,1,2,3},{4,0,1,2,3},{0,3,0,1,2,3},{7,0,6,0,1,2,3},....

when I remove 'list' from {} sampling is ok but I lose the possibility
of manipulating p .

What to do?

The general problem is : how to deal with random functions inside
module?

Please, help!

Arek


  • Prev by Date: Re: Re: indeterminate and infinity expressions
  • Next by Date: Re: StringReplace and WordBoundary
  • Previous by thread: Re: Plot Question
  • Next by thread: Re: random inside module