Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2003
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2003

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

Search the Archive

Re: errors while picking random numbers under constraint and in loop also

  • To: mathgroup at smc.vnet.net
  • Subject: [mg44177] Re: [mg44133] errors while picking random numbers under constraint and in loop also
  • From: "Peter Pein" <nospam at spam.no>
  • Date: Sat, 25 Oct 2003 06:26:28 -0400 (EDT)
  • References: <200310240824.EAA04285@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

From: "sean kim" <sean_incali at yahoo.com>
To: mathgroup at smc.vnet.net
Subject: [mg44177] [mg44133] errors while picking random numbers under constraint and
in loop also


> hello group.
>
> I'm design a routine to pick random numbers for two
> coefficients such that when first is less than the
> second coefficient, then accept, if not pick new
> random numbers. I'm also trying to put that routine in
> a loop.
>
> but I get the following errors. this always seems to
> happen when the test yields False.
>
> In[248]:=
> Label[here];
> {k7 ->  Random[Real,10^{-5, -3}],k8 ->
> Random[Real,10^{-5, -3}]};

what do you want to do with these rules??

> k7n= %[[1, 2]];
> k8n= %%[[2, 2]];

Why not k7n=Random[...]; k8n=Random[...]; and construct your rule later?

> test = TrueQ[k7n<k8n];
> If[test,{k7 -> k7n ,k8 ->
> k8n},Remove[{k7n,k8n}];Goto[here]]
>

a real brain-twister...
If [k7n<k8n"then-part","else-part"];

> Remove::ssym: {k7n, k8n} is not a symbol
>

Remove[k7n,k8n];

> Goto::nolabel: Label here not found.
>
> Out[253]= Hold[Goto[here]]
>
> now, if I try to flank the above with the For loop, i
> get the following.

I never tried Goto's and Labels since the BASIC and Fortran days in the
80's.

>
> In[181]:=
> For[ii=0, ii<10,
>
> Label[here];
>   {k7->  Random[Real,10^{-5, -3}],k8 ->
> Random[Real,10^{-5, -3}]};
>   k7n= %[[ 1, 2]];
>   k8n= %%[[ 2, 2]];
>   test = TrueQ[k7n<k8n];
>   If[test,Remove[{k7n,k8n}];Goto[here],{k7-> k7n ,k8->
> k8n}],
>
>   ii++]
>
What if the test fails 11 times?
> Part::partw: Part 2 of Goto[here] does not exist.
>
> Part::partd: Part specification True[[2, 2]] is longer
> than depth of object.
>
> above two error messages continues. until it gets
> supressed.

In your 1st version you want k7n<k8n and in your 2nd k7n>=k8n.

If you want it as a loop:

(* my hands should drop off as punishment*)
Block[{k7n,k8n,goon=True},
While[goon,
   k7n=Random[Real,10^{-5,-3}];
   k8n=Random[Real,10^{-5,-3}];
   goon= (k7n>=k8n);
];
{k7->k7n,k8->k8n}
]

>
> what am I doing wrong here?
>
Seems, you read too less in the doc. Read about functional programming or at
least program structures too.

> any and all thoughts are thoroughly appreciated.
>
> thanks in advance.
>
> sean
>
>
>
> __________________________________
> Do you Yahoo!?
> The New Yahoo! Shopping - with improved product search
> http://shopping.yahoo.com
>

Thread[Rule[{k7,k8},Sort[Table[Random[Real,10^{-5,-3}],{2}]]]]

ok, this one doesn't take care if k7n==k8n, but this is nearly impossible.

So, do not use this line of code for flying planes or to control a nuclear
power station.

this should be correct:
Block[{k7n=Random[Real,10^(-5,-3}],k8n},
  While[(k8n=Random[Real,10^(-5,-3}])<=k7n (*do nothing (else)*) ];
{k7->k7n,k8->k8n} ]

P.S.: depending on the type of your problem, it might be worth considering
10^Random[Real,{-5,-3}] instead of Random[Real,10^{-5,-3}].

Peter Pein, Berlin
petsie at arcAND.de
replace && by || to write to me


  • Prev by Date: AW: RE: 4-D plot?
  • Next by Date: Re: Baffling Failure when plotting multiple curves.
  • Previous by thread: errors while picking random numbers under constraint and in loop also
  • Next by thread: Re: errors while picking random numbers under constraint and in loop also