For my clustering gui, I am currently using random colors for the clusters, since I won't know before hand how many clusters I will end up with.
In Python, this looks like:
import random
def randomColor():
return (random.random(),random.random(),random.random())
However, when I update things, the colors change.
So what I would favor is to have a function which has an input argument I such as
def nonrandomColor(i):
...
return color
would always return the same color for the same I, while keeping the ability to generate arbitrarily many colors.
Answer does not have to be formulated in Python, it's more the general layout I'm interested in.
One way is to use caching. Use a defaultdict:
>>> import random
>>> def randomColor():
... return (random.random(),random.random(),random.random())
...
>>> from collections import defaultdict
>>> colors = defaultdict(randomColor)
>>> colors[3]
(0.10726172906719755, 0.97327604757295705, 0.58935794305308264)
>>> colors[1]
(0.48991106537516382, 0.77039712435566876, 0.73707003166893892)
>>> colors[3]
(0.10726172906719755, 0.97327604757295705, 0.58935794305308264)
Just set the seed of the random generator to the index, this might be cheaper than storing the colors.
random.seed(i)
Note that this will make random numbers way less random than before. If that is a problem, e.g. if your application uses random numbers elsewhere, you might want to look into the caching options suggested by other answers.
If you want repeatable non colliding colors then you could use something like the function below. It sections the number into 1, 10, 100 and then uses them as the RGB parts of the color.
def color(i):
r = i % 10
g = (i//10) % 10
b = (i//100) % 10
return(r*25, g*25, b*25)
For example:
color(1) == (25,0,0)
color(10) == (0,25,0)
color(999) = (225,225,255)
You want to store the colors in a dictionary or a list:
colors = {} # int -> color
def nonrandomColor(i):
if i not in colors:
colors[i] = randomColor()
return colors[i]
You can use i to seed the random number generator. So, as long as the seed remains the same, you get the same value.
>>> import random
>>> random.seed(12)
>>> random.randint(0,255), random.randint(0,255), random.randint(0,255)
(121, 168, 170)
>>> random.seed(12)
>>> random.randint(0,255), random.randint(0,255), random.randint(0,255)
(121, 168, 170)
>>> random.seed(10)
>>> random.randint(0,255), random.randint(0,255), random.randint(0,255)
(146, 109, 147)
>>> random.seed(10)
>>> random.randint(0,255), random.randint(0,255), random.randint(0,255)
(146, 109, 147)
Depending on the number of colours you're likely to generate (i.e., 10 or a million), the caching method might be better than the seed() method.
Related
I've been trying to make a program for a card game, a quick summary: Write a python script to create a random card, representing the card as a tuple, storing the card value and the color.
One of the conditions was that whilst I loop 3 times, I have to make sure that each card does not have the same values and color as the rest so that no 2 cards are the same. (When the second card is created, check if it is the same as the first and if it is, try again.
When the third card is created, check if it is the same as the first
OR the second and if it is, try again.)
I would then have to create a function that adds together the value of 3 cards and returns
the sum.
Of course, there is more to it than that but my question is a simple one. So far what I've done:
import random
num = random.randint(0,9)
colour = random.choice(["red", "green", "blue", "yellow"])
card = (num, colour)
def get_uno_card(card):
for _ in range(1):
print(card)
if num and colour == card:
print(card)
return card
def convert():
a = str(card)
return a
def add():
b = num * 3
return b
a = convert()
b = add()
print(get_uno_card(card))
print(a)
print(b)
This outputs:
(6, 'blue')
(6, 'blue')
(6, 'blue')
18
I have trouble trying to make sure the cards are not the same, therefore I was looking for ways to do this online and there was a post where they explained that I should use '/r'. I haven't come across this and don't know how I should use this in this scenario, or if I should use it at all.
Could someone explain how I could check if the 3 tuples are the same and if they are, how would I generate another card in its place?
\r is a carriage return character, and has nothing at all to do with the problem you're describing, which is one of sampling without replacement.
An easy way to make sure you don't have the same card twice is to make a "deck" of all the cards (which in Python is easily represented as a list) rather than generating each card individually.
With a real deck of cards, you can't get the same card twice because you remove the card from the deck when it's dealt; use your list the same way by popping items from it.
>>> import random
>>> cards = [(num, color) for num in range(10) for color in ["red", "green", "blue", "yellow"]]
>>> random.shuffle(cards)
>>> cards.pop()
(3, 'blue')
>>> cards.pop()
(4, 'yellow')
>>> cards.pop()
(5, 'blue')
Putting this in the context of some of the things you've tried to implement already:
import random
deck = [
(num, color)
for num in range(10)
for color in ("red", "green", "blue", "yellow")
]
random.shuffle(deck)
def get_uno_card():
return deck.pop()
def add_cards(cards):
return sum(num for num, color in cards)
cards = [get_uno_card() for _ in range(3)]
print(cards)
print(add_cards(cards))
[(9, 'yellow'), (9, 'blue'), (3, 'yellow')]
21
I'm new to Python and writing a small script to set an RGB color where two of the RGB colors are randint(0,255) and the third (selected randomly) is 0.
To acomplish this, I have this:
import time
from random import *
from neopixel import *
print ('Press Ctrl-C to quit.')
while True:
colorList = [0, randint(0,255), randint(0,255)]
shuffle(colorList)
rColor = ???
gColor = ???
bColor = ???
time.sleep(1)
I 'm having a lot of luck finding the answer but if I wanted to extract the first item from the list and set it to rColor, the second for gColor and etc, what Python function could I use to do that?
Edit: My end goal format would be an output that was simply "0, 150, 150". The NeoPixel library I am working with is picky so that is the literal format I am aiming for.
To set the color values to three variables. Use
r, g, b = colorList
or
r = colorList[0]
g = colorList[1]
b = colorList[2]
If the meaning of 'output' is print the result to console. Then use
print(*colorList, sep=', ')
If you need a string '0, 150, 150'
result = ', '.join(map(str, colorList))
or
result = ', '.join([str(x) for x in colorList])
This seems to do the trick.
from time import sleep
from random import randint, shuffle
while True:
color_list = [0, randint(0,255), randint(0,255)]
shuffle(color_list)
r_color, g_color, b_color = color_list
# sets colors to a string like "12, 10, 0"
colors = ', '.join(str(color) for color in color_list)
# print (colors)
sleep(1)
EDIT: added lines to include your edit
Psychology experiments often require you to pseudo-randomize the trial order, so that the trials are apparently random, but you don't get too many similar trials consecutively (which could happen with a purely random ordering).
Let's say that the visual display on each trial has a colour and a size:
display_list = []
colours = {0: 'red', 1: 'blue', 2: 'green', 3: 'yellow'}
sizes = [1] * 20 + [2] * 20 + [3] * 20 + [4] * 20 + [5] * 20 + [6] * 20
for i in range(120):
display_list.append({'colour': colours[i % 4], 'size': sizes[i]})
print(display_list)
And we can look at the maximum number of consecutive trials that has the same value for either property using this function:
def consecutive_properties(seq, field):
longest_run = 0
prev_value = None
current_run = 0
for d in seq:
if d[field] == prev_value:
current_run += 1
else:
current_run = 1
if current_run > longest_run:
longest_run = current_run
prev_value = d[field]
return longest_run
Output:
>>> print("Consecutive colours: ", consecutive_properties(display_list, 'colour')
('Consecutive colours: ', 1)
>>> print("Consecutive sizes: ", consecutive_properties(display_list, 'size'))
('Consecutive sizes: ', 20)
Are there any algorithms you know of that would allow minimizing the consecutive runs of either or both properties, or at least keep these runs below a specified length? If the latter, let's say no more than 4 in a row of the same colour or size.
What I've tried:
The solution I have now basically does a slightly intelligent bogosort, which has to be horribly inefficient. Basically:
You break the entire list into chunks containing all the permutations of the properties: if you break down display_list into chunks of length 24, each chunk has each colour paired with each size. Let's assume that the trial list can always be broken down into these permutation chunks, since you know what the permutations are from the design of the experiment.
You choose a maximum run length per chunk
You shuffle each chunk until the run lengths for each chunk are below the maximum value (this actually means that in the overall trial list, your runs might be double that length, since you could have a run of this length at the end of one chunk and the start of the next)
Question: Are there any algorithms you know of that would allow minimizing the
consecutive runs of either or both properties, or at least keep these
runs below a specified length?
Yes. There is an easy algorithm for doing this by simply reducing the probability of a color or size being chosen if it is already occurring in a run.
from random import randrange
def choose(colors, numselections, maxrun):
'Repeatedly choose colors. Gradually reduce selection probability to avoid runs.'
colors = list(colors)
n = len(colors)
total = n * maxrun
current_run = 0
for _ in range(numselections):
i = randrange(total - current_run) // maxrun
yield colors[i]
colors[i], colors[-1] = colors[-1], colors[i]
current_run = current_run + 1 if i==n-1 else 1
if __name__ == '__main__':
colors = ['red', 'blue', 'green', 'yellow']
for color in choose(colors, 100, maxrun=4):
print color
Note, this approach requires less effort than the other answers which use reselection techniques to avoid runs. Also, note the runs are faded-out gradually rather than all at once as in the other answers.
You're clearly not concerned with anything like true randomness, so if you define a distance metric, and draw your sequence randomly, you can reject any new draw if it's distance is "too close" to the previous draw, and simply draw again.
If you're drawing from a finite set (say, a pack of cards) then the whole set can
be the draw pile, and your sort would consist of swapping two elements when a close
pair is found, but also reject a swap partner if the swapped element would become unacceptable, so each swap step leaves the whole set improved.
If your criteria are not too hard to satisfy, this will terminate very quickly.
If the likelihood of consecutive elements is not very high (as in your example), I would simply reshuffle if the condition is not met. As you can see, most of the time you get away with one try, so it is quite efficient.
In [1]: from random import shuffle
In [2]: from itertools import groupby
In [3]: from collections import Counter
In [4]: def pseudo_shuffle(lst, limit, tries=1):
...: temp = list(lst)
...: shuffle(temp)
...: if max(sum(1 for x in g) for k, g in groupby(temp)) <= limit:
...: return tries #return temp
...: return pseudo_shuffle(lst, limit, tries=tries+1)
In [5]: colors = 30*['red', 'blue', 'green', 'yellow']
In [6]: sizes = [1] * 20 + [2] * 20 + [3] * 20 + [4] * 20 + [5] * 20 + [6] * 20
In [7]: Counter([pseudo_shuffle(colors, 4) for _ in range(1000)])
Out[7]: Counter({1: 751, 2: 200, 3: 38, 4: 10, 5: 1})
In [8]: Counter([pseudo_shuffle(sizes, 4) for _ in range(1000)])
Out[8]: Counter({1: 954, 2: 44, 3: 2})
As ddyer said, you are interested in randomness rather than sorting. My solution here would be:
Pick a random A element from your source list
Pick a random position I from your destination list
Insert A at position I to dest. list
Check if the destination list is valid. If not, restore the previous state and repeat
A working snippet:
from random import randint
from operator import itemgetter
from itertools import islice
def reshuffle(_items, max_consequent):
items = _items[:]
new_order = []
while items:
src_pos = randint(0, len(items)-1)
dest_pos = randint(0, len(new_order))
item = items[src_pos]
new_order.insert(dest_pos, item)
if is_new_order_fine(new_order, max_consequent):
items.pop(src_pos)
else:
new_order.pop(dest_pos)
return new_order
def is_new_order_fine(items, n_max):
return (
not has_consecutive_keys(items, n_max, key=itemgetter('colour')) and
not has_consecutive_keys(items, n_max, key=itemgetter('size')))
# can be optimised - don't check all items, just surrounding N
def has_consecutive_keys(items, n_max, key):
_has_n_keys = False
if len(items) >= n_max:
last_value = key(items[0])
n_consequent = 1
for item in items[1:]: # can optimize by using iterator
if key(item) == last_value:
n_consequent += 1
else:
last_value = key(item)
n_consequent = 1
if n_consequent >= n_max:
_has_n_keys = True
break
return _has_n_keys
Note that you don't need to check all items in the destination list each time, K on the left and right around the inserted new item (not implemented in the snippet)
Edit:
You can use groupby in has_consecutive_keys (but without sorting!)
Sorry, it's not an answer, but it's hard to post code in comments. Here is a simpler way to write the consecutive_properties function
from operator import itemgetter
from itertools import groupby
def consecutive_properties(seq, field):
return max(sum(1 for x in g) for k,g in groupby(seq, key=itemgetter(field)))
When I understand your question properly I'll try to turn this into an answer :)
For a Django App, each "member" is assigned a color to help identify them. Their color is stored in the database and then printed/copied into the HTML when it is needed. The only issue is that I am unsure how to generate random Hex colors in python/django. It's easy enough to generate RGB colors, but to store them I would either need to a) make three extra columns in my "Member" model or b) store them all in the same column and use commas to separate them, then, later, parse the colors for the HTML. Neither of these are very appealing, so, again, I'm wondering how to generate random Hex colors in python/django.
import random
r = lambda: random.randint(0,255)
print('#%02X%02X%02X' % (r(),r(),r()))
Here is a simple way:
import random
color = "%06x" % random.randint(0, 0xFFFFFF)
To generate a random 3 char color:
import random
color = "%03x" % random.randint(0, 0xFFF)
%x in C-based languages is a string formatter to format integers as hexadecimal strings while 0x is the prefix to write numbers in base-16.
Colors can be prefixed with "#" if needed (CSS style)
little late to the party,
import random
chars = '0123456789ABCDEF'
['#'+''.join(random.sample(chars,6)) for i in range(N)]
Store it as a HTML color value:
Updated: now accepts both integer (0-255) and float (0.0-1.0) arguments. These will be clamped to their allowed range.
def htmlcolor(r, g, b):
def _chkarg(a):
if isinstance(a, int): # clamp to range 0--255
if a < 0:
a = 0
elif a > 255:
a = 255
elif isinstance(a, float): # clamp to range 0.0--1.0 and convert to integer 0--255
if a < 0.0:
a = 0
elif a > 1.0:
a = 255
else:
a = int(round(a*255))
else:
raise ValueError('Arguments must be integers or floats.')
return a
r = _chkarg(r)
g = _chkarg(g)
b = _chkarg(b)
return '#{:02x}{:02x}{:02x}'.format(r,g,b)
Result:
In [14]: htmlcolor(250,0,0)
Out[14]: '#fa0000'
In [15]: htmlcolor(127,14,54)
Out[15]: '#7f0e36'
In [16]: htmlcolor(0.1, 1.0, 0.9)
Out[16]: '#19ffe5'
This has been done before. Rather than implementing this yourself, possibly introducing errors, you may want to use a ready library, for example Faker. Have a look at the color providers, in particular hex_digit.
In [1]: from faker import Factory
In [2]: fake = Factory.create()
In [3]: fake.hex_color()
Out[3]: u'#3cae6a'
In [4]: fake.hex_color()
Out[4]: u'#5a9e28'
Just store them as an integer with the three channels at different bit offsets (just like they are often stored in memory):
value = (red << 16) + (green << 8) + blue
(If each channel is 0-255). Store that integer in the database and do the reverse operation when you need to get back to the distinct channels.
import random
def hex_code_colors():
a = hex(random.randrange(0,256))
b = hex(random.randrange(0,256))
c = hex(random.randrange(0,256))
a = a[2:]
b = b[2:]
c = c[2:]
if len(a)<2:
a = "0" + a
if len(b)<2:
b = "0" + b
if len(c)<2:
c = "0" + c
z = a + b + c
return "#" + z.upper()
So many ways to do this, so here's a demo using "colorutils".
pip install colorutils
It is possible to generate random values in (RGB, HEX, WEB, YIQ, HSV).
# docs and downloads at
# https://pypi.python.org/pypi/colorutils/
from colorutils import random_web
from tkinter import Tk, Button
mgui = Tk()
mgui.geometry('150x28+400+200')
def rcolor():
rn = random_web()
print(rn) # for terminal watchers
cbutton.config(text=rn)
mgui.config(bg=rn)
cbutton = Button(text="Click", command=rcolor)
cbutton.pack()
mgui.mainloop()
I certainly hope that was helpful.
import secrets
# generate 4 sets of 2-digit hex chars for a color with transparency
rgba = f"#{secrets.token_hex(4)}" # example return: "#ffff0000"
# generate 3 sets of 2-digit hex chars for a non-alpha color
rgb = f"#{secrets.token_hex(3)}" # example return: "#ab12ce"
import random
def generate_color():
color = '#{:02x}{:02x}{:02x}'.format(*map(lambda x: random.randint(0, 255), range(3)))
return color
Basically, this will give you a hashtag, a randint that gets converted to hex, and a padding of zeroes.
from random import randint
color = '#{:06x}'.format(randint(0, 256**3))
#Use the colors wherever you need!
For generating random anything, take a look at the random module
I would suggest you use the module to generate a random integer, take it's modulo 2**24, and treat the top 8 bits as R, that middle 8 bits as G and the bottom 8 as B.
It can all be accomplished with div/mod or bitwise operations.
hex_digits = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
digit_array = []
for i in xrange(6):
digit_array.append(hex_digits[randint(0,15)])
joined_digits = ''.join(digit_array)
color = '#' + joined_digits
import random
def get_random_hex:
random_number = random.randint(0,16777215)
# convert to hexadecimal
hex_number = str(hex(random_number))
# remove 0x and prepend '#'
return'#'+ hex_number[2:]
Would like to improve upon this solution as I found that it could generate color codes that have less than 6 characters. I also wanted to generate a function that would create a list that can be used else where such as for clustering in matplotlib.
import random
def get_random_hex:
random_number = random.randint(0,16777215)
# convert to hexadecimal
hex_number = str(hex(random_number))
# remove 0x and prepend '#'
return'#'+ hex_number[2:]
My proposal is :
import numpy as np
def color_generator (no_colors):
colors = []
while len(colors) < no_colors:
random_number = np.random.randint(0,16777215)
hex_number = format(random_number, 'x')
if len(hex_number) == 6:
hex_number = '#'+ hex_number
colors.append (hex_number)
return colors
Here's a simple code that I wrote based on what hexadecimal color notations represent:
import random
def getRandomCol():
r = hex(random.randrange(0, 255))[2:]
g = hex(random.randrange(0, 255))[2:]
b = hex(random.randrange(0, 255))[2:]
random_col = '#'+r+g+b
return random_col
The '#' in the hexadecimal color code just represents that the number represented is just a hexadecimal number. What's important is the next 6 digits. Pairs of 2 digits in those 6 hexadecimal digits represent the intensity of RGB (Red, Green, and Blue) each. The intensity of each color ranges between 0-255 and a combination of different intensities of RGB produces different colors.
For example, in #ff00ff, the first ff is equivalent to 255 in decimal, the next 00 is equivalent to 0 in decimal, and the last ff is equivalent to 255 in decimal. Therefore, #ff00ff in hexadecimal color coding is equivalent to RGB(255, 0, 255).
With this concept, here's the explanation of my approach:
Generated intensities of random numbers for each of r, g
and b
Converted those intensities into hexadecimal
Ignored the first 2 characters of each hexadecimal value '0x'
Concatenated '#' with the hexadecimal values r, g and b
intensities.
Feel free to refer to this link if you wanna know more about how colors work: https://hackernoon.com/hex-colors-how-do-they-work-d8cb935ac0f
Cheers!
Hi, maybe i could help with the next function that generate random Hex colors :
from colour import Color
import random as random
def Hex_color():
L = '0123456789ABCDEF'
return Color('#'+ ''.join([random.choice(L) for i in range(6)][:]))
from random import randbytes
randbytes(3).hex()
output
f5f2c9
There are a lot of complex answers here, this is what I used for a code of mine using one import line and one line to get a random code:
import random
color = '#' + ''.join(random.choices('0123456789ABCDEF', k=6))
print(color)
The output will be something like:
#3F67CD
If you want to make a list of color values, let's say, of 10 random colors, you can do the following:
import random as r
hex_chars = '0123456789ABCDEF'
num_colors = 10
colors = ['#' + ''.join(r.choices(hex_chars, k=6)) for _ in range(num_colors)]
print(colors)
And the output will be a list containing ten different random colors:
['#3DBEA2', '#0B3B64', '#31D196', '#6A98C2', '#9C1712', '#73AFFE', '#9F5E0D', '#A2F07E', '#EB6407', '#7E8FB6']
I hope that helps!
For example,
The function could be something like def RandABCD(n, .25, .34, .25, .25):
Where n is the length of the string to be generated and the following numbers are the desired probabilities of A, B, C, D.
I would imagine this is quite simple, however i am having trouble creating a working program. Any help would be greatly appreciated.
Here's the code to select a single weighted value. You should be able to take it from here. It uses bisect and random to accomplish the work.
from bisect import bisect
from random import random
def WeightedABCD(*weights):
chars = 'ABCD'
breakpoints = [sum(weights[:x+1]) for x in range(4)]
return chars[bisect(breakpoints, random())]
Call it like this: WeightedABCD(.25, .34, .25, .25).
EDIT: Here is a version that works even if the weights don't add up to 1.0:
from bisect import bisect_left
from random import uniform
def WeightedABCD(*weights):
chars = 'ABCD'
breakpoints = [sum(weights[:x+1]) for x in range(4)]
return chars[bisect_left(breakpoints, uniform(0.0,breakpoints[-1]))]
The random class is quite powerful in python. You can generate a list with the characters desired at the appropriate weights and then use random.choice to obtain a selection.
First, make sure you do an import random.
For example, let's say you wanted a truly random string from A,B,C, or D.
1. Generate a list with the characters
li = ['A','B','C','D']
Then obtain values from it using random.choice
output = "".join([random.choice(li) for i in range(0, n)])
You could easily make that a function with n as a parameter.
In the above case, you have an equal chance of getting A,B,C, or D.
You can use duplicate entries in the list to give characters higher probabilities. So, for example, let's say you wanted a 50% chance of A and 25% chances of B and C respectively. You could have an array like this:
li = ['A','A','B','C']
And so on.
It would not be hard to parameterize the characters coming in with desired weights, to model that I'd use a dictionary.
characterbasis = {'A':25, 'B':25, 'C':25, 'D':25}
Make that the first parameter, and the second being the length of the string and use the above code to generate your string.
For four letters, here's something quick off the top of my head:
from random import random
def randABCD(n, pA, pB, pC, pD):
# assumes pA + pB + pC + pD == 1
cA = pA
cB = cA + pB
cC = cB + pC
def choose():
r = random()
if r < cA:
return 'A'
elif r < cB:
return 'B'
elif r < cC:
return 'C'
else:
return 'D'
return ''.join([choose() for i in xrange(n)])
I have no doubt that this can be made much cleaner/shorter, I'm just in a bit of a hurry right now.
The reason I wouldn't be content with David in Dakota's answer of using a list of duplicate characters is that depending on your probabilities, it may not be possible to create a list with duplicates in the right numbers to simulate the probabilities you want. (Well, I guess it might always be possible, but you might wind up needing a huge list - what if your probabilities were 0.11235442079, 0.4072777384, 0.2297927874, 0.25057505341?)
EDIT: here's a much cleaner generic version that works with any number of letters with any weights:
from bisect import bisect
from random import uniform
def rand_string(n, content):
''' Creates a string of letters (or substrings) chosen independently
with specified probabilities. content is a dictionary mapping
a substring to its "weight" which is proportional to its probability,
and n is the desired number of elements in the string.
This does not assume the sum of the weights is 1.'''
l, cdf = zip(*[(l, w) for l, w in content.iteritems()])
cdf = list(cdf)
for i in xrange(1, len(cdf)):
cdf[i] += cdf[i - 1]
return ''.join([l[bisect(cdf, uniform(0, cdf[-1]))] for i in xrange(n)])
Here is a rough idea of what might suit you
import random as r
def distributed_choice(probs):
r= r.random()
cum = 0.0
for pair in probs:
if (r < cum + pair[1]):
return pair[0]
cum += pair[1]
The parameter probs takes a list of pairs of the form (object, probability). It is assumed that the sum of probabilities is 1 (otherwise, its trivial to normalize).
To use it just execute:
''.join([distributed_choice(probs)]*4)
Hmm, something like:
import random
class RandomDistribution:
def __init__(self, kv):
self.entries = kv.keys()
self.where = []
cnt = 0
for x in self.entries:
self.where.append(cnt)
cnt += kv[x]
self.where.append(cnt)
def find(self, key):
l, r = 0, len(self.where)-1
while l+1 < r:
m = (l+r)/2
if self.where[m] <= key:
l=m
else:
r=m
return self.entries[l]
def randomselect(self):
return self.find(random.random()*self.where[-1])
rd = RandomDistribution( {"foo": 5.5, "bar": 3.14, "baz": 2.8 } )
for x in range(1000):
print rd.randomselect()
should get you most of the way...
Thank you all for your help, I was able to figure something out, mostly with this info.
For my particular need, I did something like this:
import random
#Create a function to randomize a given string
def makerandom(seq):
return ''.join(random.sample(seq, len(seq)))
def randomDNA(n, probA=0.25, probC=0.25, probG=0.25, probT=0.25):
notrandom=''
A=int(n*probA)
C=int(n*probC)
T=int(n*probT)
G=int(n*probG)
#The remainder part here is used to make sure all n are used, as one cannot
#have half an A for example.
remainder=''
for i in range(0, n-(A+G+C+T)):
ramainder+=random.choice("ATGC")
notrandom=notrandom+ 'A'*A+ 'C'*C+ 'G'*G+ 'T'*T + remainder
return makerandom(notrandom)