Generate random RGB color using rand () function

I need a function that will generate three numbers, so I can use them as an RGB pattern for my SVG.
Although it is simple, I also need to make sure that I do not use the same color twice. How exactly am I doing this? Create one number at a time with a simple rand (sowing time is active), and then what? I do not want to exclude a number, but maybe the whole pattern?
I'm lost here.

To be precise, the first call to this function I will get, for example, 218 199 154, and the second I will get 47 212 236, which are definitely two different colors. Any suggestions?

Also I think a struct with int r, int g, int b is suitable for this?

Edit: Colors should be different from the human eye. Sorry for not mentioning this before.

+4
source share
4 answers

You can use the set to store the generated colors. First create a new set. Then, every time you generate a color, see if a value is present in your set. If the entry exists, skip it and try again for the new color. If not, you can use it, but remember to cache it in Set after. This can become ineffective if you need to create a large number of colors.

+2
source

The cheapest way to do this is to use a Bloom filter , which is very small in memory size, but leads to random false positives (i.e., you will think that you used the color, but you don’t have it). Basically, create three random numbers between 0-255, save them as you like, hash them as a triplet and put the hash in the filter.

In addition, you can throw out the low bits of each channel, since it is probably not easy to say # FFFFF0 compared to C # FFFFF2.

+2
source

Here is an easy way:

 1.Generate a random integer. 2.Shift it 8 times to have 24 meaningful bits, store this integer value. 3.Use first 8 bits for R, second group of 8 bits for G, and the remaining 8 bits for B value. 

For each new random number, shift it 8 times, compare all the other integer values ​​that you saved earlier, if none of them matches the new one, use it for the new color (step3).

The differentiation of the human eye is an interesting topic, as thresholds of perception vary from one person to another. To achieve this, change the integer 14 times, get the first 6 bits for R (two bytes to two to get 8 bits again), get the second 6 bits for G and the last 6 bits for B. If you think that 6 bits are not are good for him, reduce him to 5.4 ...

A simple run with 4 significant bits for each channel: My random integer:

 0101-1111-0000-1111-0000-1100-1101-0000 

I shift (you can also use multiply or modulo) so that it leaves 20 times:

 0000-0000-0000-0000-0000-0101-1111-0000 

save this value.

Then get the first 4 bits for R, the second 4 bits for G and the last 4 bits for B:

 R: 0101 G: 1111 B: 0000 

Place them so that each of them consists of 8 bits.

 R: 0101-0000 G: 1111-0000 B: 0000-0000 

Use them for your color components.

For each new random number after the offset, compare it with your stored integer values. If it is different, save and use it for color.

+2
source

One idea would be to use a bit vector to represent a set of generated colors. For 24-bit precision, the bit-bit will be 2 24 in length, which is 16,777,216 bits or 2 MB. Of course, not so much these days, and it would be very fast to search and paste colors.

0
source

Source: https://habr.com/ru/post/1415972/


All Articles