## Mathematica: Applying rules to random integer generation

Find help with Math software, Engineering software, Graph software, Scientific software, Maple, Mathematica, Matlab, Pari/GP, calculators: Casio Classpad / TI Voyage / HP ...

### Mathematica: Applying rules to random integer generation

Hello

I have a problem in Mathematica. In the range 1-30 I will like Mathematica to choose five almost random integers. I can do so by writing:

RandomSample[Range[30], 5]

The thing is how to apply some rules, restricting the random command. Like this:

1) At most 2 integers =<12
2) At least one integer >=25
3) Each of the integers 3,4 and 7 can't be a divisor for more than 3 of the random chosen integers (so e.g. 3-12-15-30 or 7-21-28-14 is not possible)
4) At most one "pair of integers" (if both e.g. 3 and 4 are chosen, I call it a "pair").

Anyone with suggestions on how to solve this in Mathematica language?

thesixmax
Jack of Clubs

Posts: 45
Joined: Tue Aug 04, 2009 6:02 pm
Location: Copenhagen, Denmark

### Re: Mathematica: Applying rules to random integer generation

Write a function that checks if the conditions are met, then define a function that stores a random number in a variable and checks if the conditions are met; if so return the variable's value, if not recurse.
Pari/GP: this is the program I probably mentioned in my post. Windows users can get it at http://pari.math.u-bordeaux.fr/pub/pari ... -2-6-2.exe

CRGreathouse
Global Moderator

Posts: 12539
Joined: Sat Nov 25, 2006 9:52 am
Location: UTC -5

### Re: Mathematica: Applying rules to random integer generation

CRGreathouse wrote:Write a function that checks if the conditions are met, then define a function that stores a random number in a variable and checks if the conditions are met; if so return the variable's value, if not recurse.

I am not totally sure on how to understand this. But I have made a try:

chosen = Sort[RandomSample[Range[36], 7], Less];
less12 = Count[Select[chosen, # <= 12 &], _Integer];
great31 = Count[Select[chosen, # > 31 &], _Integer];
divisor = Count[Select[chosen, Divisible[#, {3, 8}] &], _Integer];
test = If[less12 == 2,
If[great31 == 1, If[divisor <= 5, chosen, False], False], False]

If the random generation is False, then the elements in the list either contains more than two integers <12, no integers >31, or more than five elements with 3-8 as a divisable integer. If every condition is met, I get the list generated in "chosen". The thing is - I now want to loop the procedure, so I don't have to run it manually a lot of times to get anything other than False. I have thought about the while command. It will have to be something like:

1) Check if "test" is true
2) If yes -> print list. If no-> make a new random sample and repeat the test.

Any input on this?

thesixmax
Jack of Clubs

Posts: 45
Joined: Tue Aug 04, 2009 6:02 pm
Location: Copenhagen, Denmark

### Re: Mathematica: Applying rules to random integer generation

thesixmax wrote:Any input on this?

Once you have a function that will generate one random number of the sort you want, just use Table or the like to come up with a bunch of them.
Pari/GP: this is the program I probably mentioned in my post. Windows users can get it at http://pari.math.u-bordeaux.fr/pub/pari ... -2-6-2.exe

CRGreathouse
Global Moderator

Posts: 12539
Joined: Sat Nov 25, 2006 9:52 am
Location: UTC -5

### Re: Mathematica: Applying rules to random integer generation

CRGreathouse wrote:Once you have a function that will generate one random number of the sort you want, just use Table or the like to come up with a bunch of them.

Okay, I have found the right way to produce a loop, so that Mathematica will list a random list generation based on the conditions mentioned before:

Code: Select all
`list = Sort[RandomSample[Range[36], 7]];f[x_] := If[Count[Select[list, # <= 12 &], _Integer] == 2,  If[Count[Select[list, # > 31 &], _Integer] == 1,   If[Count[Select[list, Divisible[#, {3, 8}] &], _Integer] <= 5,     list, False], False], False]While[f[x] == False, list = Sort[RandomSample[Range[36], 7], Less];  If[list == f[x], a]]f[x]`

First, a random list is made. Then f checks if the conditions are met. If that is the case, f equals list, False otherwise. The "While" makes a loop. In the case f=False, a new list is produced. This procedure is made until the list equals f, and f is printed.
Now the last challenge: This procedure only produces one result. How to make the first 5 results appear at one time?

thesixmax
Jack of Clubs

Posts: 45
Joined: Tue Aug 04, 2009 6:02 pm
Location: Copenhagen, Denmark

### Re: Mathematica: Applying rules to random integer generation

thesixmax wrote:First, a random list is made. Then f checks if the conditions are met. If that is the case, f equals list, False otherwise. The "While" makes a loop. In the case f=False, a new list is produced. This procedure is made until the list equals f, and f is printed.

That's not what I suggested, but I suppose it works. (You can cut out a whole step by replacing "False" with f[x] -- then you wouldn't need the While loop.) Also I'd drop the dummy variable x, which you don't use. If I properly understand your code it's doing a huge amount more work than it needs to.

thesixmax wrote:Now the last challenge: This procedure only produces one result. How to make the first 5 results appear at one time?

Table.
Pari/GP: this is the program I probably mentioned in my post. Windows users can get it at http://pari.math.u-bordeaux.fr/pub/pari ... -2-6-2.exe

CRGreathouse
Global Moderator

Posts: 12539
Joined: Sat Nov 25, 2006 9:52 am
Location: UTC -5

### Re: Mathematica: Applying rules to random integer generation

CRGreathouse wrote:Table.

You are right about the function. I tried not to include the loop, but I got a huge amount of errors, so I decided to include it anyway. You mention table. I am not sure how you would use it? How could I make sure that different results appear? Table have to execute the function 5 times, but I can't understand how to do that.

thesixmax
Jack of Clubs

Posts: 45
Joined: Tue Aug 04, 2009 6:02 pm
Location: Copenhagen, Denmark

### Re: Mathematica: Applying rules to random integer generation

This is the most basic possible use of the function. If you want a 5 random numbers as generated by g[], you just write List[g[], 5].
Pari/GP: this is the program I probably mentioned in my post. Windows users can get it at http://pari.math.u-bordeaux.fr/pub/pari ... -2-6-2.exe

CRGreathouse
Global Moderator

Posts: 12539
Joined: Sat Nov 25, 2006 9:52 am
Location: UTC -5

### Re: Mathematica: Applying rules to random integer generation

CRGreathouse wrote:This is the most basic possible use of the function. If you want a 5 random numbers as generated by g[], you just write List[g[], 5].

I now have the code:

Code: Select all
`list = RandomSample[Range[36], 7];f := If[1 <= Count[Select[list, # <= 12 &], _Integer] <= 2,  If[Count[Select[list, # > 31 &], _Integer] >= 1,   If[Count[Select[list, Divisible[#, {2, 7}] &], _Integer] <= 5,     list, False], False], False]; While[f == False,  list = RandomSample[Range[36], 7];  If[list == f, f]]f`

Example output is:
Code: Select all
`{1, 7, 15, 16, 25, 28, 34}`
. Then I want to list the first 5 runs. I try something like:

Code: Select all
`Table[Sort[f], {i, 5}]`

but the thing I get is
Code: Select all
`{1, 7, 15, 16, 25, 28, 34}, {1, 7, 15, 16, 25, 28, 34}, {1, 7, 15, 16, 25, 28, 34}, {1, 7, 15, 16, 25, 28, 34},{1, 7, 15, 16, 25, 28, 34}`

I think the problem is that Table does not ask the loop to run 5 times. The loop itself does not come up with a value, but defines the value for my original function f. I can't figure out how to solve that problem.

thesixmax
Jack of Clubs

Posts: 45
Joined: Tue Aug 04, 2009 6:02 pm
Location: Copenhagen, Denmark

### Re: Mathematica: Applying rules to random integer generation

You need tro wrap everything that generates the random numbers into a single function and run that repeatedly. If you generate it with one call and simply test it it will be the same each time.
Pari/GP: this is the program I probably mentioned in my post. Windows users can get it at http://pari.math.u-bordeaux.fr/pub/pari ... -2-6-2.exe

CRGreathouse
Global Moderator

Posts: 12539
Joined: Sat Nov 25, 2006 9:52 am
Location: UTC -5

### Re: Mathematica: Applying rules to random integer generation

CRGreathouse wrote:You need tro wrap everything that generates the random numbers into a single function and run that repeatedly. If you generate it with one call and simply test it it will be the same each time.

I managed to get it to work! Final code:

Code: Select all
`f[list_] := And[  1 <= Count[Select[list, # <= 12 &], _Integer] <= 2,   Count[Select[list, # > 31 &], _Integer] >= 1,   Count[Select[list, Or @@ Divisible[#, Range[2, 7]] &], _Integer] <= 3]Block[{n = 0, list},  Reap[While[n < 5, list = Sort@RandomSample[Range[36], 7];     If[f[list], n++; Sow[list]]]]][[2, 1]]`

Thanks for the help throughout the problem.

thesixmax
Jack of Clubs

Posts: 45
Joined: Tue Aug 04, 2009 6:02 pm
Location: Copenhagen, Denmark

### Re: Mathematica: Applying rules to random integer generation

This thread inspired me to blog about another solution to the problem. You can find my thoughts here http://mathematicacookbook.com/?p=119. Even if you stick with the solution presented in this thread, my post suggests slightly cleaner way to implement the function f[list_](the Selects inside Count are not necessary if you use /;).

mathematicacookbook
Newcomer

Posts: 1
Joined: Thu Jan 12, 2012 10:46 pm

### Re: Mathematica: Applying rules to random integer generation

Very nice! Yes, my Mathematica skills are very basic.
Pari/GP: this is the program I probably mentioned in my post. Windows users can get it at http://pari.math.u-bordeaux.fr/pub/pari ... -2-6-2.exe

CRGreathouse
Global Moderator

Posts: 12539
Joined: Sat Nov 25, 2006 9:52 am
Location: UTC -5