Jump to content

PHP help

xampi

Hi guys,

I want to create a STRING that contains numbers from 1 to 10 without repetition. How can I do that? (This is what I have) (I'm very newbie in programming).

 

    <?php
        for($i=0;$i<=9;$i++){
            $rannumber='';
            $rannumber.= mt_rand(1, 10);
            print($rannumberi);
        }
    ?>

 

Edited by LogicalDrm
Link to comment
Share on other sites

Link to post
Share on other sites

Moved to Programming forum. 

CPU: Intel i7 6700k  | Motherboard: Gigabyte Z170x Gaming 5 | RAM: 2x16GB 3000MHz Corsair Vengeance LPX | GPU: Gigabyte Aorus GTX 1080ti | PSU: Corsair RM750x (2018) | Case: BeQuiet SilentBase 800 | Cooler: Arctic Freezer 34 eSports | SSD: Samsung 970 Evo 500GB + Samsung 840 500GB + Crucial MX500 2TB | Monitor: Acer Predator XB271HU + Samsung BX2450

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, xampi said:

Hi guys,

I want to create a STRING that contains numbers from 1 to 10 without repetition. How can I do that? (This is what I have) (I'm very newbie in programming).

 

    <?php
        for($i=0;$i<=9;$i++){
            $rannumber='';
            $rannumber.= mt_rand(110);
            print($rannumberi);
        }
    ?>

not really familar with php so I'm kind guessing the syntax

   for($i=1;$i<=10;$i++){
            $rannumber='';
            $rannumber.=  $i;
            print($rannumber);
        }

Ignore me. terrible answer lol 

 

That what u want? 

 

$rannumber='';
  for($i=1;$i<=10;$i++){
            
            $rannumber.=  $i;
            
        }
print($rannumber);
 

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, werto165 said:

not really familar with php so I'm kind guessing the syntax

   for($i=1;$i<=10;$i++){
            $rannumber='';
            $rannumber.=  i;
            print($rannumber);
        }

Ignore me. terrible answer lol 

The thing is that I want numbers from 1 to 10 randomly generated with no repetitions.

 

I need them to be random, not from 1 to 10 thanks to $i but thanks for the help!

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, xampi said:

The thing is that I want numbers from 1 to 10 randomly generated with no repetitions.

 

Your syntax repeats numbers but thanks for the help!

a single string? if so you'll need a global scope for your variable. random isn't really random per se. from what I remember it takes the current time on ur CPU clock and from that passes it into a formula to give u a number. so a random number each time could be 11111111 not 12456 if that makes some sense. If you don't want them to repeat you will need to perform a check that those values aren't already used so if the first random number is 1 then that can't be used again for the random number. 

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, werto165 said:

a single string? if so you'll need a global scope for your variable. random isn't really random per se. from what I remember it takes the current time on ur CPU clock and from that passes it into a formula to give u a number. so a random number each time could be 11111111 not 12456 if that makes some sense. 

Look I have something more elaborated that doesn't work. I hope it helps you understand what i'm trying to get:

 

        // $numbers = '';
        // for($i=0;$i<=9;$i++){
        //     $rannumberi= '';
        //     $rannumber.= mt_rand(1, 10);
        //     // if(strstr($numbers, $rannumber) == FALSE) {
        //         print($numbers).'<br>';
        //         $numbers.= $rannumber;
        //     // }else{
        //         // $i = $i - 1;
        //     // }
        // }

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, homeap5 said:

Use array, populate it with numbers 1, 2, 3... and then replace first value in array with random value from array, then second with random etc.

I can't do it with any array. It's an exercice from school.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, xampi said:

I can't do it with any array. It's an exercice from school.

if you can't use an array and need to use random you will have to use a while loop. A for loop will not work because it may run the same number twice and you wont get a string that's 11 characters long. 

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
Share on other sites

Link to post
Share on other sites

<?php
$r="0123456789";
for($t=0;$t<10;$t++){
$b=rand(0,9);
$v1=substr($r,$t,1);
$v2=substr($r,$b,1);
$r=substr_replace($r,$v1,$b,1);
$r=substr_replace($r,$v2,$t,1);
}
echo($r);
?>

Done, without array.

If all you need is numbers from 0 to 9 to be mixed in single text variable.

Link to comment
Share on other sites

Link to post
Share on other sites

28 minutes ago, homeap5 said:

Done, without array.

If all you need is numbers from 0 to 9 to be mixed in single text variable.

If you want to confuse a newbie go for recursion with ternary. 

<?php
function recursiveRandomNumStr($n, $str = '') {
    return (0 === $n) ? $str : recursiveRandomNumStr(--$n, $str .= mt_rand(0, 9));
}
echo recursiveRandomNumStr(10);

 

1 hour ago, xampi said:

Hi guys,

I want to create a STRING that contains numbers from 1 to 10 without repetition. How can I do that? (This is what I have) (I'm very newbie in programming).

 

    <?php
        for($i=0;$i<=9;$i++){
            $rannumber='';
            $rannumber.= mt_rand(110);
            print($rannumberi);
        }
    ?>

Your mistake is that you initialise rannumber and print it out inside the cycle

<?php
$rannumber='';
for($i=0;$i<=9;$i++){
    $rannumber.= mt_rand(0, 9);
} 
print($rannumber);

 

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, pd. said:

If you want to confuse a newbie go for recursion with ternary.

I give a proper solution - all numbers must not repeat but being in random order.

Your example is not working like that.

OP Said: "I want to create a STRING that contains numbers from 1 to 10 without repetition."

I also understand that not so good as I see, because it's not 0 to 9, but that is not big difference - he may replace 0 with 10 in results and display every number in separate line. I think that is why OP start with empty string inside loop and print every number separately.

 

So, small fix for my code:

<?php
$r="0123456789";
for($t=0;$t<10;$t++){
$b=rand(0,9);
$v1=substr($r,$t,1);
$v2=substr($r,$b,1);
$r=substr_replace($r,$v1,$b,1);
$r=substr_replace($r,$v2,$t,1);
}

for($t=0;$t<10;$t++){
$rr=substr($r,$t,1);
if ($rr==0) $rr="10";
echo($rr."<br>");
}
?>

This will display all numbers from 1 to 10 in separate lines, in random order. And "0" is replaced by "10".

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Sakuriru said:

 


<?php

$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
shuffle($array);

foreach($array as $n) {
    echo $n . "\n";
}


 

2 hours ago, xampi said:

I can't do it with any array. It's an exercice from school.

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Sakuriru said:

if the teacher wants to be pedantic

<?php
$start = 1;
$stop = 10;
for ($i = $start; $i <= $stop; $i++) {
    $$i = $i;
}
for ($i = $start; $i <= $stop; $i++) {
    $m = mt_rand($start, $stop);
    $tmp = ${$m};
    $$m = ${$i};
    $$i = $tmp;
}
for ($i = $start; $i <= $stop; $i++) {
    echo ${$i} . ' ';
}

No arrays, no string, works for any range

BTW, @xampi this is probably not the answer that your teacher expects. Even if it works, this is an ugly solution that basically uses variable variables as a subtitute for arrays.

Edited by pd.
Added warning
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×