Home python Different colors python

Different colors python

Author

Date

Category

I have a problem. I need to create a colors list, in which every three values ​​are color RGB .

That is, the first three list item is for example: 234 56 145 . Where 234: Number of red, 56 green, 145 blue.

All numbers must be random from 0 to 255 .
It is necessary to do so that all the colors did not repeat.
That is, there was no two identical RGB values.
Preferably give an example with code.


Answer 1, Authority 100%

So you did not accurately formulated the question, then the answer to it is trivial.
If this requires exactly,

you need to do so that all the colors did not repeat. That is, there is no two identical RGB values.

The only way your way is to achieve color non-repeating – to generate all possible combinations of colors and then extract as many samples from this set as it is necessary.

Make it can, for example, like this:

import random
zzz = []
For i in Range (256):
  For j in Range (256):
    For k in Range (256):
      zzz.append ([i, j, k])
Random.Sample (ZZZ, 5)

or like this:

import itertools
ZZZ = LIST (Itertools.Product ([i for i in range (256)], [j for j in range (256)], [k for k in range (256)], repeat = 1))
Random.Sample (ZZZ, 5)

If you just make the random.sample (Range (x), 3), then we will receive repetitions. Here is an example – 15 random samples from 27 possible combinations

import random
For i in Range (15):
  Print (RANDOM.SAMPLE (RANGE (3), 3))

Result:

[1, 0, 2]
[2, 0, 1]
[0, 2, 1]
[1, 2, 0]
[1, 0, 2]
[0, 2, 1]
[1, 2, 0]
[2, 0, 1]
[0, 2, 1]
[1, 0, 2]
[2, 1, 0]
[2, 0, 1]
[2, 1, 0]
[0, 1, 2]
[1, 2, 0]

repeats more than enough.


Answer 2, Authority 50%

Try this:

import random
Random.sample (Range (255), 3)

Answer 3

offer and I have my own funny way. First code:

from random import shuffle
DEF Get256Random ():
  L = list (Range (256))
  SHUFFLE (L)
  Return L.
DEF Get256RGB ():
  R256 = Get256RANDOM ()
  G256 = Get256Random ()
  B256 = get256RANDOM ()
  RGB = [(R, G, B) for (R, G, B) in Zip (R256, G256, B256)]
  Return RGB.
S = set ()
L = []
For i in Range (10,000):
  RGB = Get256RGB ();
  RGB_Uniq = [(R, G, B) for (R, G, B) in RGB IF NOT (R, G, B) in S]
  L.APPEND (LEN (RGB_UNIQ))
  S.UPDATE (RGB_UNIQ)
Print (L [: 10])
Print (L.INDEX (255))
Print (L [-10:])
Print (Len (S), Len (S) / (10000 * 256))

My get255rgb function is guaranteed to give 256 different color combinations. It can be reused, to give a pack of 256 new non-repetitive colors, but all the following packs must already be checked – there have been no such colors in previous packs. In the variable RGB_UNIQ , another pack of colors are selected, which have not yet met. At the end of the check – and on what iteration the repeats start? And how many repetitions happens after 10,000 cycles?

[256, 256, 256, 256, 256, 256, 256, 256, 256, 256]
15
[216, 213, 219, 219, 225, 227, 211, 234, 232, 224]
2374357 0.927483203125

So, at the beginning of the repetition was not – 56 unique colors were issued.

The first repetition met at the 16th iteration.

After 10,000 cycles, there was already an average of 20% repeats.

A total of 2374357 unique colors were generated, the repeats total was less than 8%.

But it is necessary to remember that it is a rand and numbers with each start can be the others.


Answer 4

If without repetition, then you can try this:

import random
for i in range (1):
   spisok = []
   a = random.randint (0, 256)
   b = random.randint (0, 256)
   c = random.randint (0, 256)
   if a! = b and a! = c and b! = c:
     spisok.append (a)
     spisok.append (b)
     spisok.append (c)
     print (spisok)

I apologize in advance for the illiteracy of the code, since I have little experience.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions