Random Numbers
yeah that was basically exactly what I would suggest.
Here's rough code, untested.
36 Replies
Ok, I'm back to this. Any idea why after 14 to 19 executions, this just adds
undefined
to pickedNumbers
?let's see
well you exclude 50-150
so that leaves 0-49 and 151-360
(which btw why does it go up to 360 and not 256)
Hue is in degrees
ah
using hue
gotcha
okay so that's 50 + 210 = 260 possibilities left unexcluded
BUT x excludes at least...
30 possibilities (less than or equal to 15)
Ok, that makes sense since
260/15
is 17.3right
it's because you're basically running out of valid colours to pick from
after you've generated enough hues for a full colour palette you can reset allowedNumbers to the initial value I showed
I guess I can just "loop" the colors. i.e. after 14 colors, it starts reusing colors
well it starts reusing the domain of allowed hues
it's still going to be generating randomly
it's just that you're excluding hues close to ones you've generated before
meaning you run out fast
It would be best to have a dynamic tolerance for closeness. i.e. only check closeness of the last color generated
This would work work then, I believe.
I'd honestly write out allAllowedHues in a full loop and functional form, I just wrote it like that because I was doing it fast rather than nice
That kept returning
0
, so I switched the return value to chosenHue
, but now it's just 16
and 21
.ah sorry you're right about chosenHueIndex -> chosenHue lemme look at the problem now
what's your baseHue?
0
I tried with
76
and now it's returning 1
and 6
lol
probably got the filter wrong
me that is
try this:
Nice! These seem perfect
yeah I had just a typo or two
happens when I don't run the code and refactor it lol
Ok, so this is my code now
and in the
scsApp
class:
It seems to be that I'll need to add support for two "lastHue" values or else I will get alternating top/bottom's that are the sameI'd simplify it to:
as for lastHues
honestly it might be good practice for you to figure out a way yourself, no?
I mean I'm not opposed necessarily to helping you if you're stuck
Sure, I'm not here asking for help xD
haha
just progress reports
gotcha
looks good so far though
I might ask later though 🙂
definitely do
Is there a reason why you put
!= null
?Habit I suppose but I don't like the boolean casting
Ok, I thought it was "cleaner". I'll do it that way if it's better
It's an opinion but
if (!x)
checks if it's falsy so could be expanded like so: if (x === false || x === "" || x === NaN || x === 0 || x === null || x === undefined)
there's actually a handful more but idr of the top of my head
whereas you probably just mean if (x === undefined || x === null)
which if (x != null)
'expands' toWell, there's an issue if it equals to
NaN
, ""
, or false
as well...right which is why it's an opinion haha
I honestly prefer it explode in my face rather than reinitialise
because if I have like x is an integer
I have to remember the
if (!x)
will trigger when it's 0
and go back to refactor the conditionsActually, shouldn't it be?
don't you only filter if the lastHue isn't null?
here's what I mean
also wait
ah nvm
hue goes from 1-360 not 0-360
nvm
if it included 0 you definitely couldn't do
if (x)
In my code here, I only filter if lastHue is truthy
which translates to
or this
right?
oh, nvm
yeah I was trying to write up an explanation but you probably figured it out
Yeah, I was mixed up, but you are right about it being
!= null
since that's the same as == true
well not exactly
but close
let x;
is actually the same as let x = undefined;
but we also have null
let x = null
if (x == null)
will trigger whenever x is set to either undefined or null
(because double equals is weird)