Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    rand() su 5numeri che devono essere diversi

    Devo scegliere 5 numeri compresi tra 1 e 10 random doversi...c'è un modo apparte controllare tramite cicli if abbastanza lunghini?

    Grazie.
    GdR Online - http://lenar.it/
    Yesterday is history. Tomorrow is mystery.
    Today is a gift. That's why it's called the present

  2. #2

    Re: rand() su 5numeri che devono essere diversi

    potresti fare cosi:
    Codice PHP:
    <?php

    /* riempio l'array con i valori possibili*/
    $v=array();
    for(
    $i=1$i<=10$i++){
        
    $v[]=$i;
    }

    /*estraggo 5 indici dell'array random*/
    $keys array_rand($v5);

    /* stampo i valori estratti */
    foreach($keys as $k ){
        echo 
    "{$v[$k]}\n";
        
    }

    ?>

  3. #3
    Moderatore di Server Apache L'avatar di marketto
    Registrato dal
    Sep 2001
    Messaggi
    5,858
    oppure:
    codice:
    $temp=range(1,10);
    srand((double)microtime()*123456789);
    shuffle($temp);
    for($i=0;$i<5;$i++)
    	echo array_shift($temp)." ";
    think simple think ringo

  4. #4
    i numeri sarebbero in

    $k[1],$k[2],$k[3],$k[4],$k[5] ?

    Grazie.
    GdR Online - http://lenar.it/
    Yesterday is history. Tomorrow is mystery.
    Today is a gift. That's why it's called the present

  5. #5
    Ok ok...funza.

    Grazie!
    GdR Online - http://lenar.it/
    Yesterday is history. Tomorrow is mystery.
    Today is a gift. That's why it's called the present

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.