For Programmers: Free Programming Magazines  


Home > Archive > Mathematica > August 2006 > Re: Random numbers according to a rule / function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Random numbers according to a rule / function
Bob Hanlon

2006-08-25, 4:05 am

conditionalRandom[test_Function, type_:Integer, range_]:=
Module[{r},
While[Not[test[r=Random[type,range]]]];
r];

test1=(Mod[#,13]==0&&Mod[#,19]==0)&;

a=conditionalRandom[test1,{100,1000}]

494

test1[a]

True

test2=PrimeQ[#]&&Total[IntegerDigits[#]]==10&;

b=conditionalRandom[test2,{1,10000}]

9001

test2[b]

True


Bob Hanlon

---- bd satish <bdsatish@gmail.com> wrote:
> Hi buddies ,
>
> I want to write a module / program that generates random
> numbers according to some rule (or function).
>
> Say , "A number that is divisible by 13 & 19" or "A prime number whose sum
> of digits is exactly 10" , etc.
>
> Let me call this module as RandomRule[ f ]. Everytime I evaluate this
> function , I must get a number 'x' such that f [x] is True.
>
> For different invokes , RandomRule[ f ] must give different values. There
> are no restrictions on the function ' f ' .
>
> For simplicity, let ' x ' be an integer.
>
> This is what I tried :
>
> "A number that is divisible by 13 and 19"
>
> I put the command
>
> (Mod [ # , 14 ] == 0 && Mod[ # , 19 ] ==0 )&
> Random[Integer,{100,1000}]
>
> in a Do loop until the above statement returns True. And returned the value
> of Random[Integer,{100,1000}]
> that made this statement True.
>
> My problem is , how can I generalize this to include ANY function ' f ' ?
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com