phpBB2Refugees.com Logo
Not affiliated with or endorsed by the phpBB Group

Register •  Login 

Continue the legacy...

Welcome to all phpBB2 Refugees!Wave Smilie

This site is intended to continue support for the legacy 2.x line of the phpBB2 bulletin board package. If you are a fan of phpBB2, please, by all means register, post, and help us out by offering your suggestions. We are primarily a community and support network. Our secondary goal is to provide a phpBB2 MOD Author and Styles area.

How to call a function multiple times / mt_rand for letters?


 
Search this topic... | Search MOD Writing... | Search Box
Register or Login to Post    Index » MOD Writing  Previous TopicPrint TopicNext Topic
Author Message
Acaria
Board Member



Joined: 20 Feb 2009

Posts: 238



PostPosted: Sun Oct 25, 2009 4: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



PostPosted: Sun Oct 25, 2009 7: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


flag
PostPosted: Sun Oct 25, 2009 5:52 pm 
Post subject: Re: How to call a function multiple times / mt_rand for lett

Take a look at the chr() function, and maybe you will see how to integrate that with mt_rand(). This ASCII table should also help.
_________________
Moof! Email me: dog_cow@macgui.com
Inside Allerton Park Lincoln's Tomb, Oak Ridge Cemetery, Springfield IL Mac 512K Blog
Back to top
Acaria
Board Member



Joined: 20 Feb 2009

Posts: 238



PostPosted: Sun Oct 25, 2009 7: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: 621
Location: Spain


flag
PostPosted: Sun Oct 25, 2009 10: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. icon_mrgreen.gif
_________________
phpBB2 will never die, I hope!
Back to top
Murmur
Board Member



Joined: 20 Aug 2009

Posts: 57
Location: California


flag
PostPosted: Mon Oct 26, 2009 5: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



PostPosted: Mon Oct 26, 2009 5: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


flag
PostPosted: Mon Oct 26, 2009 6: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



PostPosted: Mon Oct 26, 2009 6: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



PostPosted: Mon Oct 26, 2009 6: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


flag
PostPosted: Mon Oct 26, 2009 6: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



PostPosted: Mon Oct 26, 2009 7:06 am 
Post subject: Re: How to call a function multiple times / mt_rand for lett

OMG, thank you thank you thank you! icon_biggrin.gif

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


flag
PostPosted: Mon Oct 26, 2009 7: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



PostPosted: Mon Oct 26, 2009 1:34 pm 
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


flag
PostPosted: Mon Oct 26, 2009 8: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
Display posts from previous:   
Register or Login to Post    Index » MOD Writing  Previous TopicPrint TopicNext Topic
Page 1 of 1 All times are GMT
 
Jump to:  

Index • About • FAQ • Rules • Privacy • Search •  Register •  Login 
Not affiliated with or endorsed by the phpBB Group
Powered by phpBB2 © phpBB Group
Generated in 0.0221 seconds using 15 queries. (SQL 0.0024 Parse 0.0007 Other 0.0191)
phpBB Customizations by the phpBBDoctor.com
Template Design by DeLFlo and MomentsOfLight.com Moments of Light Logo