Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use random to choose colors

Tags:

I'm trying to make a little program to learn more, but am stuck when it comes to using random.

Here is an example of what I'm going off of https://trinket.io/Python/3338c95430

I've tried using random.randrange, random.choice, random.random with everything for them and it sends an error code saying random doesn't have a function of randrange, choice, or random.

import turtle, math, random, time  wn = turtle.Screen() wn.bgcolor('grey') Rocket = turtle.Turtle() Rocket.speed(0) Rocket.color('red') ## this is what i want to randomize rotate=int(90)  def drawCircles(t,size):     for i in range(15):         t.circle(size)         size=size-10 def drawSpecial(t,size,repeat):     for i in range(repeat):         drawCircles(t,size)         t.right(360/repeat) drawSpecial(Rocket,100,10) 

Eventually I would like to implement more randomized processes like the size and placement but for now I'm just focusing on color.

like image 298
jman8453 Avatar asked Jun 15 '19 02:06

jman8453


People also ask

How do you select a random color in python?

Using random() function to generate random colors To begin, import the random function in Python to obtain a random color. The variable r stands for red, g stands for green, and b stands for blue. We already know that the RGB format contains an integer value ranging from 0 to 255.

How do I select a random color in CSS?

var randomColor = Math. floor(Math. random()*16777215).


1 Answers

Without using additional imports it's fairly simple:

turtle.colormode(255) # sets the color mode to RGB  R = random.randrange(0, 256, 100) # last value optional (step)  B = random.randrange(0, 256) G = random.randrange(0, 256)  # using step allows more control if needed # for example the value of `100` would return `0`, `100` or `200` only  Rocket.color(R, G, B) ## randomized from values above 

Using randomized values of (200,255,23):

enter image description here

EDIT: Regarding "would i just change the turtle.colormode() to Rocket.colormode() for the next one?"

The way I would recommend doing it would be to create a function:

def drawColor():      turtle.colormode(255)      R = random.randrange(0, 256)     B = random.randrange(0, 256)     G = random.randrange(0, 256)      return R, G, B  Rocket.color(drawColor()) 

This way you can call drawColor() anytime you want a new color.

Now that you have the basics of randomizing colors for your drawing you can get quite creative with the values for some awesome looking results (adjust integers to your liking):

enter image description here

#!/usr/bin/python  import turtle, math, random, time  def drawColor(a, b, o):     turtle.colormode(255)     R = random.randrange(a, b, o) # last value is step (optional)     B = random.randrange(a, b, o)     G = random.randrange(a, b, o)     # print(R, G, B)     return R, G, B  def drawRocket(offset):     Rocket = turtle.Turtle()     Rocket.speed(0)     Rocket.color(drawColor(20, 100, 1)) # start (0-256), end (0-256), offset      rotate=int(random.randrange(90))     drawSpecial(Rocket,random.randrange(0, 10), offset)  def drawCircles(t,size):     for i in range(30):         t.circle(size)         size = size - 20  def drawSpecial(t,size,repeat):     for i in range(repeat):         drawCircles(t,size)         t.right(360/repeat)  def drawMain(x, y):     wn = turtle.Screen()     wn.bgcolor(drawColor(0, 20, 2))      for i in range(3): # iterations         drawRocket(x)         x+=y         # print(x)  drawMain(2, 10) # offset, step input("Press ENTER to exit") 
like image 174
l'L'l Avatar answered Sep 25 '22 12:09

l'L'l