MathGroup Archive 2003

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

Search the Archive

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

From: "sean kim" <sean_incali at>
To: mathgroup at
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


> 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

> 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*)
   goon= (k7n>=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


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