Randomize character positions in string

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

    Randomize character positions in string

    I would like to randomly reorder the characters in a string.
    I wrote a function breaking the string into arrays,
    assigning Rangoon numbers and reassembling the string.

    Is there an easier way?

    Thanks,

    gsb


  • Alejandro Pedraza

    #2
    Re: Randomize character positions in string

    What about this?

    for ($i=0;$i<strlen ($string);$i++) {
    $randPos=rand(0 ,strlen($string )-1);
    $temp=$string[$i];
    $string[$i]=$string[$randPos];
    $string[$randPos]=$temp;
    }



    "gsb" <gsb@QWest.ne t> wrote in message news:<Q0NDb.45$ kH5.100762@news .uswest.net>...[color=blue]
    > I would like to randomly reorder the characters in a string.
    > I wrote a function breaking the string into arrays,
    > assigning Rangoon numbers and reassembling the string.
    >
    > Is there an easier way?
    >
    > Thanks,
    >
    > gsb[/color]

    Comment

    • Nikolai Chuvakhin

      #3
      Re: Randomize character positions in string

      "gsb" <gsb@QWest.ne t> wrote in message
      news:<Q0NDb.45$ kH5.100762@news .uswest.net>...[color=blue]
      >
      > I would like to randomly reorder the characters in a string.
      > I wrote a function breaking the string into arrays,
      > assigning Rangoon numbers and reassembling the string.
      >
      > Is there an easier way?[/color]



      Cheers,
      NC

      Comment

      • Rahul Anand

        #4
        Re: Randomize character positions in string

        If you are using PHP ver >= 4.3.0

        try using:
        $str = "This is a test string";
        $str = str_shuffle($st r)

        If your PHP ver < 4.3.0

        $str = "This is a test string";
        $sarray = preg_split('//', $str);
        unset($sarray[count($sarray)-1],$sarray[0]);
        srand((float)mi crotime()*10000 00);
        shuffle($sarray );
        $str = implode("",$sar ray);

        hope it will help.

        -- Rahul

        "gsb" <gsb@QWest.ne t> wrote in message news:<Q0NDb.45$ kH5.100762@news .uswest.net>...[color=blue]
        > I would like to randomly reorder the characters in a string.
        > I wrote a function breaking the string into arrays,
        > assigning Rangoon numbers and reassembling the string.
        >
        > Is there an easier way?
        >
        > Thanks,
        >
        > gsb[/color]

        Comment

        Working...