
Math Software Math Software  Mathematica, Matlab, Calculators, Graphing Software 
 LinkBack  Thread Tools  Display Modes 
October 12th, 2011, 06:02 PM  #1 
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 130 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. 3121530 or 7212814 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: 12,819 Thanks: 84  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  
Joined: Aug 2009 From: Copenhagen, Denmark Posts: 45 Thanks: 0  Re: Mathematica: Applying rules to random integer generation Quote:
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 38 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: 12,819 Thanks: 84  Re: Mathematica: Applying rules to random integer generation Quote:
 
October 14th, 2011, 05:28 AM  #5  
Joined: Aug 2009 From: Copenhagen, Denmark Posts: 45 Thanks: 0  Re: Mathematica: Applying rules to random integer generation Quote:
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] 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: 12,819 Thanks: 84  Re: Mathematica: Applying rules to random integer generation Quote:
Quote:
 
October 14th, 2011, 06:02 AM  #7  
Joined: Aug 2009 From: Copenhagen, Denmark Posts: 45 Thanks: 0  Re: Mathematica: Applying rules to random integer generation Quote:
 
October 14th, 2011, 06:55 AM  #8 
Global Moderator Joined: Nov 2006 From: UTC 5 Posts: 12,819 Thanks: 84  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  
Joined: Aug 2009 From: Copenhagen, Denmark Posts: 45 Thanks: 0  Re: Mathematica: Applying rules to random integer generation Quote:
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 Code: {1, 7, 15, 16, 25, 28, 34} Code: Table[Sort[f], {i, 5}] 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}  
October 14th, 2011, 09:08 AM  #10 
Global Moderator Joined: Nov 2006 From: UTC 5 Posts: 12,819 Thanks: 84  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.

October 15th, 2011, 04:41 AM  #11  
Joined: Aug 2009 From: Copenhagen, Denmark Posts: 45 Thanks: 0  Re: Mathematica: Applying rules to random integer generation Quote:
Code: 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]]  
January 13th, 2012, 07:01 PM  #12 
Joined: Jan 2012 Posts: 1 Thanks: 0  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 /.

January 14th, 2012, 08:00 PM  #13 
Global Moderator Joined: Nov 2006 From: UTC 5 Posts: 12,819 Thanks: 84  Re: Mathematica: Applying rules to random integer generation
Very nice! Yes, my Mathematica skills are very basic. 

Tags 
applying, generation, integer, mathematica, random, rules 
Thread Tools  
Display Modes  

Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Random Number Generation  PrayForStan  Number Theory  1  November 28th, 2012 06:08 AM 
random number generation with normal distribution  nsvcivil  Advanced Statistics  2  January 9th, 2012 04:18 PM 
Mathematica : random function which gives single integer?  Cham  Math Software  6  September 9th, 2011 07:31 PM 
Mathematica: export text file with increasing integer  Cham  Math Software  0  September 8th, 2011 01:32 PM 
Random numbers generation... can they repeat?  edu  Advanced Statistics  18  April 6th, 2009 05:45 PM 