My Math Forum Mathematica: Applying rules to random integer generation

 Math Software Math Software - Mathematica, Matlab, Calculators, Graphing Software

 October 12th, 2011, 06:02 PM #1 Member   Joined: Aug 2009 From: Copenhagen, Denmark Posts: 45 Thanks: 0 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?
 October 12th, 2011, 08:11 PM #2 Global Moderator     Joined: Nov 2006 From: UTC -5 Posts: 16,046 Thanks: 932 Math Focus: Number theory, computational mathematics, combinatorics, FOM, symbolic logic, TCS, algorithms 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.
October 13th, 2011, 05:42 PM   #3
Member

Joined: Aug 2009
From: Copenhagen, Denmark

Posts: 45
Thanks: 0

Re: Mathematica: Applying rules to random integer generation

Quote:
 Originally Posted by CRGreathouse 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?

October 13th, 2011, 09:41 PM   #4
Global Moderator

Joined: Nov 2006
From: UTC -5

Posts: 16,046
Thanks: 932

Math Focus: Number theory, computational mathematics, combinatorics, FOM, symbolic logic, TCS, algorithms
Re: Mathematica: Applying rules to random integer generation

Quote:
 Originally Posted by thesixmax 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.

October 14th, 2011, 05:28 AM   #5
Member

Joined: Aug 2009
From: Copenhagen, Denmark

Posts: 45
Thanks: 0

Re: Mathematica: Applying rules to random integer generation

Quote:
 Originally Posted by CRGreathouse 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:
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?

October 14th, 2011, 05:51 AM   #6
Global Moderator

Joined: Nov 2006
From: UTC -5

Posts: 16,046
Thanks: 932

Math Focus: Number theory, computational mathematics, combinatorics, FOM, symbolic logic, TCS, algorithms
Re: Mathematica: Applying rules to random integer generation

Quote:
 Originally Posted by thesixmax 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.

Quote:
 Originally Posted by thesixmax Now the last challenge: This procedure only produces one result. How to make the first 5 results appear at one time?
Table.

October 14th, 2011, 06:02 AM   #7
Member

Joined: Aug 2009
From: Copenhagen, Denmark

Posts: 45
Thanks: 0

Re: Mathematica: Applying rules to random integer generation

Quote:
 Originally Posted by CRGreathouse 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.

 October 14th, 2011, 06:55 AM #8 Global Moderator     Joined: Nov 2006 From: UTC -5 Posts: 16,046 Thanks: 932 Math Focus: Number theory, computational mathematics, combinatorics, FOM, symbolic logic, TCS, algorithms 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].
October 14th, 2011, 07:10 AM   #9
Member

Joined: Aug 2009
From: Copenhagen, Denmark

Posts: 45
Thanks: 0

Re: Mathematica: Applying rules to random integer generation

Quote:
 Originally Posted by CRGreathouse 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:
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:
{1, 7, 15, 16, 25, 28, 34}
. Then I want to list the first 5 runs. I try something like:

Code:
Table[Sort[f], {i, 5}]
but the thing I get is
Code:
{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.

 October 14th, 2011, 09:08 AM #10 Global Moderator     Joined: Nov 2006 From: UTC -5 Posts: 16,046 Thanks: 932 Math Focus: Number theory, computational mathematics, combinatorics, FOM, symbolic logic, TCS, algorithms 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.

 Tags applying, generation, integer, mathematica, random, rules

 Thread Tools Display Modes Linear Mode

 Similar Threads Thread Thread Starter Forum Replies Last Post PrayForStan Number Theory 1 November 28th, 2012 06:08 AM nsvcivil Advanced Statistics 2 January 9th, 2012 04:18 PM Cham Math Software 6 September 9th, 2011 07:31 PM Cham Math Software 0 September 8th, 2011 01:32 PM edu Advanced Statistics 18 April 6th, 2009 05:45 PM

 Contact - Home - Forums - Top