Jump to content

xampi

Member
  • Posts

    36
  • Joined

  • Last visited

Reputation Activity

  1. Like
    xampi reacted to werto165 in PHP 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. 
  2. Like
    xampi reacted to homeap5 in PHP help   
    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.
  3. Like
    xampi reacted to werto165 in PHP help   
    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. 
  4. Like
    xampi reacted to homeap5 in PHP help   
    <?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.
  5. Like
    xampi reacted to pd. in PHP help   
    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);  
    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);  
  6. Like
    xampi reacted to homeap5 in PHP help   
    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".
  7. Like
    xampi reacted to pd. in PHP help   
  8. Like
    xampi reacted to pd. in PHP help   
    <?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.
  9. Like
    xampi got a reaction from LogicWeasel in CPU Compatibility   
    Thanks for the help!
  10. Like
    xampi got a reaction from Falkentyne in PC Crashing   
    Cool. Thanks
  11. Like
    xampi reacted to Falkentyne in PC Crashing   
    Ouch.  This looks like a bad motherboard.
    slot 2+3 working but slot 1+4 not working definitely points to motherboard (Note: the RAM will be running in single channel mode in slot 2+3 and slot 1+4).
     
    My bet is on a bad motherboard here.  Otherwise slot 2+3 would have failed.

    Anyone else have any ideas let me know.
×