473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

permutations, patterns, and probability

Greetings,

I am working on a program to produce patterns. What would like is for
it to exhaustively produce all possible permutations of a sequence of
items but for each permutation produce variations, and also a sort of
stutter based on probability / weighted randomess.

Let us say we have tiles of four primary colors: ['Red', 'Blue',
'Green', 'Yellow']. Now we also have 4 alternatives or substitutes for
each color ['Maroon', 'Navy_Blue', 'Forest_Green', 'Dark_Brown']

We pick a unique permutation, say: ['Red', 'Blue', 'Yellow', 'Green']

Now I would like to pick the primary colors substitute (say 30% chance
for each element) so instead of our plain

['Red', 'Blue', 'Yellow', 'Green']

we might end up with:

['Red', 'Navy_Blue', 'Yellow', 'Forest_Green']

or

['Maroon', 'Navy_Blue', 'Yellow', 'Green']

Whatever... The main point is that sometimes the original color is
retained and sometimes the dark color is substituted.

Now I want to take this list and sometimes stutter an element so that
there is, let us say a 50% chance for each element, that it is
stuttered, and it may be repeated 1 (34%), 2(66%), or 3(33%) times. So
that we could get:

['Maroon','Maroon','Navy_Blue', 'Yellow','Yellow','Yellow','Yellow',
'Green']

The program would quit when all 24 (in the case of 4 elements) was
exhausted.

I have code that makes weighted randomness. I have code that makes
permutations, but I am having trouble putting this all together...
While i work on it though that i might ask for help... I'd like for the
code to be reusable and am building a library of functions for
patterns.

cheers,
kevin
### This is not mine, it is from a python book... I believe the Lutz
book

def permute(list):
if not list: # shuffle any
sequence
return[list] # empty
sequence
else:
res = []
for i in range(len(list)):
rest = list[:i] + list[i+1:] # delete
current node
for x in permute(rest): # permute the
others
res.append(list[i:i+1] + x) # add node at
front
return res

mport random

### This this is mine, but seems to work anyway hee hee

def windex(lst):
'''an attempt to make a random.choose() function that makes
weighted choices

accepts a list of tuples with the item and probability as a
pair
like: >>> x = [('one', 0.25), ('two', 0.25), ('three', 0.5)]
y=windex(x)'''

n = random.uniform(0, 1)
for item, weight in lst:
if n < weight:
break
n = n - weight
return item

Jul 18 '05 #1
1 2291
kpp9c wrote:
Greetings,

I am working on a program to produce patterns. What would like is for
it to exhaustively produce all possible permutations of a sequence of
items but for each permutation produce variations, and also a sort of
stutter based on probability / weighted randomess.

Let us say we have tiles of four primary colors: ['Red', 'Blue',
'Green', 'Yellow']. Now we also have 4 alternatives or substitutes for
each color ['Maroon', 'Navy_Blue', 'Forest_Green', 'Dark_Brown']

We pick a unique permutation, say: ['Red', 'Blue', 'Yellow', 'Green']

Now I would like to pick the primary colors substitute (say 30% chance
for each element) so instead of our plain

['Red', 'Blue', 'Yellow', 'Green']

we might end up with:

['Red', 'Navy_Blue', 'Yellow', 'Forest_Green']

or

['Maroon', 'Navy_Blue', 'Yellow', 'Green']

Whatever... The main point is that sometimes the original color is
retained and sometimes the dark color is substituted.

Now I want to take this list and sometimes stutter an element so that
there is, let us say a 50% chance for each element, that it is
stuttered, and it may be repeated 1 (34%), 2(66%), or 3(33%) times. So
that we could get:

['Maroon','Maroon','Navy_Blue', 'Yellow','Yellow','Yellow','Yellow',
'Green']

The program would quit when all 24 (in the case of 4 elements) was
exhausted.


Playing around with this:

py> def alt_color(color, color_map=dict(Red='Maroon',
.... Blue='Navy_Blue',
.... Yellow='Forest_Green',
.... Green='Dark_Brown')):
.... if random.random() <= 0.3:
.... return color_map[color]
.... return color
....
py> def reps():
.... if random.random() < 0.5:
.... return 1
.... return random.randint(2, 4)
....
py> def combinations(items, n):
.... if n==0:
.... yield []
.... else:
.... for i in xrange(len(items)):
.... item_slice = items[i:i+1]
.... for c in combinations(items[:i]+items[i+1:], n-1):
.... yield item_slice + c
....
py> colors = ['Red', 'Blue', 'Yellow', 'Green']
py> some_colors = combinations(colors, len(colors)).next()
py> some_colors
['Red', 'Blue', 'Yellow', 'Green']
py> alt_colors = [alt_color(c) for c in some_colors]
py> alt_colors
['Red', 'Navy_Blue', 'Yellow', 'Green']
py> rep_colors = [c for color in alt_colors for c in [color]*reps()]
py> rep_colors
['Red', 'Red', 'Navy_Blue', 'Navy_Blue', 'Navy_Blue', 'Yellow', 'Green',
'Green']

Hope some of that is helpful.

Steve
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
20
by: John Trunek | last post by:
I have a set of X items, but want permutations of length Y (X > Y). I am aware of the permutation functions in <algorithm>, but I don't believe this will do what I want. Is there a way, either...
2
by: Andreas Schmitt | last post by:
Hi, Sorry for posting in German before, totally forgot about that when I was pasting this in here from another German newsgroup I was writing to, trying to get help I am programming a simple...
13
by: Frode Řijord | last post by:
Hi all, given a sequence of n elements i need to generate all possible permutations of length k <= n. I found an elegant way to do this recursively: def comb(items, n): if n==0: yield ...
8
by: Jim Hubbard | last post by:
I am looking for patterns for a distributed .Net application for a small retail chain. The owner wants the stores to have access to all data (no matter which store it comes from) in real time. ...
20
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
1
by: JosAH | last post by:
Greetings, last week we talked a bit about generating permutations and I told you that this week will be about combinations. Not true; there's a bit more to tell about permutations and that's...
82
by: Bill Cunningham | last post by:
I don't know if I'll need pointers for this or not. I wants numbers 10^16. Like a credit card 16 digits of possible 10 numbers, so I guess that would be 10^16. So I have int num ; These are of...
19
by: fera | last post by:
Hi to all of you guys here… A friend of mine gave me: 1). A paper with a table of 350 rows x 284 columns, which each cell contains of a single number from 0 to 9. This table didn’t typed yet into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.