Author |
Message |
Acaria Board Member
Joined: 20 Feb 2009
Posts: 238
|
Posted: Sun Oct 25, 2009 12:05 am Post subject: How to call a function multiple times / mt_rand for letters? |
|
|
Hi guys. I've been working on a new project and have hit a little bump.
I've written a function that generates a random color. However, to make a palette, I need to do this function multiple times. There's got to be a way to just call the function, like, 6 or 7 times without copying or pasting. ; ~;
Also, is there an mt_rand for letters? XD
It's just that a lot of colors have letters in there and I want to have a more expansive palette. |
|
Back to top |
|
|
Acaria Board Member
Joined: 20 Feb 2009
Posts: 238
|
Posted: Sun Oct 25, 2009 3:16 am Post subject: Re: How to call a function multiple times / mt_rand for lett |
|
|
Actually, would something like this work to get letters from an mt_rand?
Code: |
function getPaletteDigit
{
$paletterandom = mt_rand(1,2)
if $paletterandom == 1
{
$palettedigit = mt_rand(0,9);
}
if $paletterandom == 2
{
function getPaletteLetters
{
$paletteletterrandom = mt_rand(1,6);
if $paletteletterrandom == 1
{
$paletteletter = 'a';
}
if $paletteletterrandom == 2
{
$paletteletter = 'b';
}
if $paletteletterrandom == 3
{
$paletteletter = 'c';
}
if $paletteletterrandom == 4
{
$paletteletter = 'd';
}
if $paletteletterrandom == 5
{
$paletteletter = 'e';
}
if $paletteletterrandom == 6
{
$paletteletter = 'f';
}
}
$palettedigit = $paletteletter
}
}
|
|
|
Back to top |
|
|
Dog Cow Board Member
Joined: 18 Nov 2008
Posts: 378
|
|
Back to top |
|
|
Acaria Board Member
Joined: 20 Feb 2009
Posts: 238
|
Posted: Sun Oct 25, 2009 3:42 pm Post subject: Re: How to call a function multiple times / mt_rand for lett |
|
|
Dog Cow, you're a god. *bows to*
Code: |
function getPaletteDigit()
{
$paletterandom = mt_rand(1,2);
if ($paletterandom == 1)
{
$palettedigit = mt_rand(0,9);
}
else if ($paletterandom == 2)
{
$paletteletterrandom = mt_rand(65,70);
$palettedigit = chr($paletteletterrandom);
}
}
|
[Edit]
Hmm... When I have the function tags around it, I can't call the $palettedigit variable. When I removed them, I could.
So, how do I get to the variable when it's in the function? |
|
Back to top |
|
|
dogs and things Board Member
Joined: 18 Nov 2008
Posts: 628 Location: Spain
|
Posted: Sun Oct 25, 2009 6:29 pm Post subject: Re: How to call a function multiple times / mt_rand for lett |
|
|
Quote: | Dog Cow, you're a god. | Actually, his true name is God Cow. _________________ phpBB2 will never die, I hope! |
|
Back to top |
|
|
Murmur Board Member
Joined: 20 Aug 2009
Posts: 57 Location: California
|
Posted: Mon Oct 26, 2009 1:26 am Post subject: Re: How to call a function multiple times / mt_rand for lett |
|
|
I'm not perfectly clear on what you're trying to accomplish but if it's a random color in web-friendly, six-digit hex format...
i.e. #000000 (black) to #FFFFFF (white) then
$myint = mt_rand(0,16777215); // integer returned
$hexcolor = dechex($myint); // hex string returned
Using this it would be necessary to prepend leading zeros for a small number to end up with six hex digits... |
|
Back to top |
|
|
Acaria Board Member
Joined: 20 Feb 2009
Posts: 238
|
Posted: Mon Oct 26, 2009 1:49 am Post subject: Re: How to call a function multiple times / mt_rand for lett |
|
|
Okay, I really need to learn all of these functions. ._.
Thank you so much, Murmur! It does exactly what I need.
But now how can I get it to do this a certain amount of times? Like say you typed "12" into a box, clicked a button, and 12 random hex codes were generated. |
|
Back to top |
|
|
Murmur Board Member
Joined: 20 Aug 2009
Posts: 57 Location: California
|
Posted: Mon Oct 26, 2009 2:18 am Post subject: Re: How to call a function multiple times / mt_rand for lett |
|
|
Without knowing more about what you're doing...I don't know. But there's two basic options for handling this sort of objective.
1) call the function 12 (or X) times from within a loop and fill an array OR
2) move the loop into the function and have it return an array of X colors.
BTW, here's a long-hand form of the function which breaks it down into the individual Red-Green-Blue components for readability. It should return a properly formatted web color.
Code: |
function gen_rand_webcolor() {
$color = array()
$i = mt_rand(0,255);
if($i < 16)
$color['red'] = '0' . dechex($i);
else
$color['red'] = dechex($i);
$i = mt_rand(0,255);
if($i < 16)
$color['green'] = '0' . dechex($i);
else
$color['green'] = dechex($i);
$i = mt_rand(0,255);
if($i < 16)
$color['blue'] = '0' . dechex($i);
else
$color['blue'] = dechex($i);
return $color['red'] . $color['green'] . $color['blue'];
} //function not tested
|
|
|
Back to top |
|
|
Acaria Board Member
Joined: 20 Feb 2009
Posts: 238
|
Posted: Mon Oct 26, 2009 2:33 am Post subject: Re: How to call a function multiple times / mt_rand for lett |
|
|
I'm working on a site related to Pixel Art. I'm using PhpBB2 as a base, since there are a few Mods I know I want there.
Pixel Art is all about specific color palettes. Each palette has a set amount of colors. I want there to be a little page where you can go, put in how many colors you want, and it'll give you that many random colors for you to work with.
/what I'm planning |
|
Back to top |
|
|
Acaria Board Member
Joined: 20 Feb 2009
Posts: 238
|
Posted: Mon Oct 26, 2009 2:43 am Post subject: Re: How to call a function multiple times / mt_rand for lett |
|
|
Okay, so I tried this:
Code: |
$palettecolorswanted = 16;
for($i = 0; $i < $palettecolorswanted; $i++)
{
$paletteint = mt_rand(0,16777215);
$palettehex = dechex($myint);
echo "$palettehex";
}
|
It only gives me 16 zeros... ; ~; |
|
Back to top |
|
|
Murmur Board Member
Joined: 20 Aug 2009
Posts: 57 Location: California
|
Posted: Mon Oct 26, 2009 2:59 am Post subject: Re: How to call a function multiple times / mt_rand for lett |
|
|
Code: |
$palettecolorswanted = 16;
for($i = 0; $i < $palettecolorswanted; $i++)
{
$paletteint = mt_rand(0,16777215);
$palettehex[$i] = dechex($paletteint);
}
for($i = 0; $i < $palettecolorswanted; $i++)
{
echo "$palettehex[$i]";
}
|
|
|
Back to top |
|
|
Acaria Board Member
Joined: 20 Feb 2009
Posts: 238
|
Posted: Mon Oct 26, 2009 3:06 am Post subject: Re: How to call a function multiple times / mt_rand for lett |
|
|
OMG, thank you thank you thank you!
Just one question: Why does it need to be $palettehex[$i]?
I understand all but that part (which makes me feel really happy, because just a month or two ago I'd be looking at that and going "LOLWUT?!" XD). |
|
Back to top |
|
|
Murmur Board Member
Joined: 20 Aug 2009
Posts: 57 Location: California
|
Posted: Mon Oct 26, 2009 3:51 am Post subject: Re: How to call a function multiple times / mt_rand for lett |
|
|
I mean no offense, Acaria, but it sounds to me like you need to learn about how arrays work in PHP. It's a necessary skill.
Arrays are a difficult concept at first. Instead of a variable like $myvariable containing one value, an array of $myvariable[] can contain many values where the [] indexes the particular value. An array variable is a 'table' of values.
Look into arrays. If you don't get it at first, keep at it. Some where along the line it will all suddenly click. But you definitely need to understand arrays. Persistence and a desire to learn are the keywords here.
And if you have questions, people here can help. |
|
Back to top |
|
|
Acaria Board Member
Joined: 20 Feb 2009
Posts: 238
|
Posted: Mon Oct 26, 2009 9:34 am Post subject: Re: How to call a function multiple times / mt_rand for lett |
|
|
Pssh, no offense taken. This thing here is actually the first time I've ever written a loop/really understood how they work. ._. |
|
Back to top |
|
|
Murmur Board Member
Joined: 20 Aug 2009
Posts: 57 Location: California
|
Posted: Mon Oct 26, 2009 4:13 pm Post subject: Re: How to call a function multiple times / mt_rand for lett |
|
|
It looks like you're making good progress. Keep it up. |
|
Back to top |
|
|
|