My Math Forum  

Go Back   My Math Forum > Math Forums > Math Software

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

Reply
 
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 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 is offline  
 
October 12th, 2011, 08:11 PM   #2
Global Moderator
 
CRGreathouse's Avatar
 
Joined: Nov 2006
From: UTC -5

Posts: 12,860
Thanks: 94

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.
CRGreathouse is online now  
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:
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?
thesixmax is offline  
October 13th, 2011, 09:41 PM   #4
Global Moderator
 
CRGreathouse's Avatar
 
Joined: Nov 2006
From: UTC -5

Posts: 12,860
Thanks: 94

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.
CRGreathouse is online now  
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:
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?
thesixmax is offline  
October 14th, 2011, 05:51 AM   #6
Global Moderator
 
CRGreathouse's Avatar
 
Joined: Nov 2006
From: UTC -5

Posts: 12,860
Thanks: 94

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.
CRGreathouse is online now  
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:
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.
thesixmax is offline  
October 14th, 2011, 06:55 AM   #8
Global Moderator
 
CRGreathouse's Avatar
 
Joined: Nov 2006
From: UTC -5

Posts: 12,860
Thanks: 94

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].
CRGreathouse is online now  
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:
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.
thesixmax is offline  
October 14th, 2011, 09:08 AM   #10
Global Moderator
 
CRGreathouse's Avatar
 
Joined: Nov 2006
From: UTC -5

Posts: 12,860
Thanks: 94

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.
CRGreathouse is online now  
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:
Originally Posted by CRGreathouse
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:
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 is offline  
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 /.
mathematicacookbook is offline  
January 14th, 2012, 08:00 PM   #13
Global Moderator
 
CRGreathouse's Avatar
 
Joined: Nov 2006
From: UTC -5

Posts: 12,860
Thanks: 94

Re: Mathematica: Applying rules to random integer generation

Very nice! Yes, my Mathematica skills are very basic.
CRGreathouse is online now  
Reply

  My Math Forum > Math Forums > Math Software

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





Copyright © 2014 My Math Forum. All rights reserved.