MathGroup Archive 2006

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

Search the Archive

Re: random inside module

  • To: mathgroup at smc.vnet.net
  • Subject: [mg67744] Re: random inside module
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Thu, 6 Jul 2006 06:52:52 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 7/5/06 at 4:17 AM, Arkadiusz.Majka at telekomunikacja.pl (Arkadiusz
Majka) wrote:

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

<snip>

It appears you are trying to create a list where every pth number is a random integer between 1 and 10 with a total length of a with some predefined initial list appended to the end. If I have this right, there is a much simpler (IMO) way to create this which doesn't use Module

specifically

maja[a_, p_, init_] := Flatten@{Table[0, {Mod[
  a, p]}], {Random[Integer, {1, 10}], #} & /@ Table[0, {Floor[a/
      p]}, {p - 1}], init}
      
seems to do what you are trying to do

For example

In[54]:=
maja[6,2,Range@3]

Out[54]=
{10,0,8,0,6,0,1,2,3}

or

In[55]:=
maja[14,3,Range@3]

Out[55]=
{0,0,6,0,0,6,0,0,9,0,0,8,0,0,1,2,3}
--
To reply via email subtract one hundred and four


  • Prev by Date: RE:Problem in evaluating functions from my own package!!!
  • Next by Date: Re: Plot Question
  • Previous by thread: random inside module
  • Next by thread: Re: random inside module