Need a string of random numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TristaSD

    #1

    Need a string of random numbers

    Hi,

    Need a string of random numbers from 1 to 6 generated by php, none of
    the numbers repeat, e.g. 142635, and NOT 114255.

    Thanks.

  • Chung Leong

    #2
    Re: Need a string of random numbers


    TristaSD wrote:[color=blue]
    > Hi,
    >
    > Need a string of random numbers from 1 to 6 generated by php, none of
    > the numbers repeat, e.g. 142635, and NOT 114255.
    >
    > Thanks.[/color]

    str_shuffle(imp lode('', range(1, 6)));

    Comment

    • Bent Stigsen

      #3
      Re: Need a string of random numbers

      Chung Leong wrote:
      [color=blue]
      >
      > TristaSD wrote:[color=green]
      >> Hi,
      >>
      >> Need a string of random numbers from 1 to 6 generated by php, none of
      >> the numbers repeat, e.g. 142635, and NOT 114255.
      >>
      >> Thanks.[/color]
      >
      > str_shuffle(imp lode('', range(1, 6)));[/color]

      Subtle hint? :)

      Comment

      • Chung Leong

        #4
        Re: Need a string of random numbers


        Bent Stigsen wrote:[color=blue]
        > Chung Leong wrote:
        >[color=green]
        > >
        > > TristaSD wrote:[color=darkred]
        > >> Hi,
        > >>
        > >> Need a string of random numbers from 1 to 6 generated by php, none of
        > >> the numbers repeat, e.g. 142635, and NOT 114255.
        > >>
        > >> Thanks.[/color]
        > >
        > > str_shuffle(imp lode('', range(1, 6)));[/color]
        >
        > Subtle hint? :)[/color]

        A to-the-point post desires a to-the-point reply ;-)

        Comment

        • R. Rajesh Jeba Anbiah

          #5
          Re: Need a string of random numbers

          Bent Stigsen wrote:[color=blue]
          > Chung Leong wrote:[/color]
          <snip>[color=blue][color=green]
          > > str_shuffle(imp lode('', range(1, 6)));[/color]
          >
          > Subtle hint? :)[/color]

          echo str_shuffle('12 3456');
          print_r(range(1 , 6));
          echo implode('', range(1, 6));

          --
          <?php echo 'Just another PHP saint'; ?>
          Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

          Comment

          • Bent Stigsen

            #6
            Re: Need a string of random numbers

            R. Rajesh Jeba Anbiah wrote:
            [color=blue]
            > Bent Stigsen wrote:[color=green]
            >> Chung Leong wrote:[/color]
            > <snip>[color=green][color=darkred]
            >> > str_shuffle(imp lode('', range(1, 6)));[/color]
            >>
            >> Subtle hint? :)[/color]
            >
            > echo str_shuffle('12 3456');
            > print_r(range(1 , 6));
            > echo implode('', range(1, 6));[/color]

            All work and no fun makes Jack a dull boy.

            I think Trista would have figured it out, once looking in the manual.

            /Bent

            Comment

            • TristaSD

              #7
              Re: Need a string of random numbers

              Thanks, all. Here's how I used this. If I weren't so lazy, I'd count
              the number of pics in a folder so that a user can just upload a pic and
              not mess with the $total_number variable in the code :)

              <?
              $total_number = 6; // Total number of pics

              for ($i = 1; $i <= $total_number; $i++) {
              $string_to_shuf fle = $string_to_shuf fle . $i;
              }

              $string_to_shuf fle = str_shuffle ($string_to_shu ffle);

              for ($counter = 0; $counter <= strlen ($string_to_shu ffle) - 1;
              $counter++) {
              echo ("<img src='images/gaslogo_");
              echo (substr ($string_to_shu ffle, $counter, 1));
              echo (".jpg' alt='some ALT text' ");
              echo (substr ($string_to_shu ffle, $counter, 1));
              echo ("' /><br />\n");
              }
              ?>

              Also, is it a good practice to break echo's like that, or should I
              write them as one echo statement (sorry for the drift) ?

              Comment

              • Chung Leong

                #8
                Re: Need a string of random numbers

                TristaSD wrote:[color=blue]
                > Thanks, all. Here's how I used this. If I weren't so lazy, I'd count
                > the number of pics in a folder so that a user can just upload a pic and
                > not mess with the $total_number variable in the code :)
                >
                > <?
                > $total_number = 6; // Total number of pics
                >
                > for ($i = 1; $i <= $total_number; $i++) {
                > $string_to_shuf fle = $string_to_shuf fle . $i;
                > }
                >
                > $string_to_shuf fle = str_shuffle ($string_to_shu ffle);
                >
                > for ($counter = 0; $counter <= strlen ($string_to_shu ffle) - 1;
                > $counter++) {
                > echo ("<img src='images/gaslogo_");
                > echo (substr ($string_to_shu ffle, $counter, 1));
                > echo (".jpg' alt='some ALT text' ");
                > echo (substr ($string_to_shu ffle, $counter, 1));
                > echo ("' /><br />\n");
                > }
                > ?>
                >
                > Also, is it a good practice to break echo's like that, or should I
                > write them as one echo statement (sorry for the drift) ?[/color]

                If you do it the first way you will go blind. If you do it the second
                way, the leprechauns will get you.

                Comment

                Working...