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];

>
> 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?
>
least program structures too.

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

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