Pagina 1 di 4 1 2 3 ... ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 32
  1. #1

    Generari 15 numeri casuali tutti diversi

    Ho un piccolo problema, ho neccessita di generare 15 numeri casuali, possibilmente in asp ma va bene anche in JS.
    Il Problema che mi sta bloccando e che questi numeri DEVONO essere compressi tra 1 e 90 (estremi inclusi) e devono neccessariamente essere tutti diversi, mai ripetuti all'interno della stessa generazione.

    Grazie a tutti del supporto!

  2. #2
    Utente di HTML.it L'avatar di ganesha
    Registrato dal
    Jan 2003
    Messaggi
    357
    prova così:

    codice:
    	var rnd=[];
    	for(var i=0;i<15;i++){ 
    		var tmp = Math.floor(Math.random()*90)+1;
    		var nuovo = true;
    		for(var j=0; j<i; j++){
    			if(rnd[j]==tmp)nuovo=false;
    		}
    		if(nuovo){
    			rnd[i]=tmp;
    		}else{
    			i--;
    		}
    	}

  3. #3
    grazie ganesha , funziona,

    un'altro problema

    <input name="1" type="text" id="1" /> deve stampare il primo numero
    <input name="2" type="text" id="2" /> deve stampare il secondo numero

    ecc.. fino a 15

    come devo fare?

  4. #4
    Utente di HTML.it L'avatar di ganesha
    Registrato dal
    Jan 2003
    Messaggi
    357
    prova così:

    for(var i=0;i<15;i++){
    document.getElementById(i+1).value = rnd[i];
    }


  5. #5
    dove lo devo aggiungere scusami?



    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Documento senza titolo</title>
    <SCRIPT LANGUAGE=JavaScript>
    var rnd=[];
    for(var i=0;i<3;i++){
    var tmp = Math.floor(Math.random()*90)+1;
    var nuovo = true;
    for(var j=0; j<i; j++){
    if(rnd[j]==tmp)nuovo=false;
    }
    if(nuovo){
    rnd[i]=tmp;
    }else{
    i--;
    }
    }
    </SCRIPT>
    </head>

    <body>
    <table width="500" border="0">
    <tr>
    <td><script type="text/javascript">document.write(rnd)</script></td>
    </tr>
    <tr>
    <td><form id="numeri" name="numeri" method="post" action="">
    <input name="1" type="text" id="1" />
    <input name="2" type="text" id="2" />
    <input name="3" type="text" id="3" />
    </form>
    </td>
    </tr>
    </table>
    </body>
    </html>

  6. #6
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    ricordo che gli id non devo essere numerici cambiali cosi id="_2"
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  7. #7
    ho provato cosi , dove ho sbagliato?

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Documento senza titolo</title>
    <SCRIPT LANGUAGE=JavaScript>
    var rnd=[];
    for(var i=0;i<3;i++){
    document.getElementById(i+1).value = rnd[i];
    }

    var tmp = Math.floor(Math.random()*90)+1;
    var nuovo = true;
    for(var j=0; j<i; j++){
    if(rnd[j]==tmp)nuovo=false;
    }
    if(nuovo){
    rnd[i]=tmp;
    }else{
    i--;
    }
    }
    </SCRIPT>
    </head>

    <body>
    <table width="500" border="0">
    <tr>
    <td><script type="text/javascript">document.write(rnd)</script></td>
    </tr>
    <tr>
    <td><form id="numeri" name="numeri" method="post" action="">
    <input name="_1" type="text" id="_1" />
    <input name="_2" type="text" id="_2" />
    <input name="_3" type="text" id="_3" />
    </form>
    </td>
    </tr>
    </table>
    </body>
    </html>

  8. #8
    Utente di HTML.it L'avatar di ganesha
    Registrato dal
    Jan 2003
    Messaggi
    357
    codice:
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Documento senza titolo</title>
    <SCRIPT LANGUAGE=JavaScript>
    	var rnd=[];
    	for(var i=0;i<15;i++){ 
    		var tmp = Math.floor(Math.random()*90)+1;
    		var nuovo = true;
    		for(var j=0; j<i; j++){
    			if(rnd[j]==tmp)nuovo=false;
    		}
    		if(nuovo){
    			rnd[i]=tmp;
    		}else{
    			i--;
    		}
    }
    for(var i=0;i<3;i++){
     document.getElementById("_"+(i+1)).value = rnd[i]; 
    }
    
    </SCRIPT>
    </head>
    
    <body>
    <table width="500" border="0">
      <tr>
        <td><script type="text/javascript">document.write(rnd)</script></td>
      </tr>
      <tr>
        <td><form id="numeri" name="numeri" method="post" action="">
          		<input name="_1" type="text" id="_1" />
                <input name="_2" type="text" id="_2" />
                <input name="_3" type="text" id="_3" />
        </form>
        </td>
      </tr>
    </table>
    </body>
    </html>

  9. #9
    ho provato , non funziona,

    vorrei fare un'altro modo se e possibile,

    Esempio :

    <script type="text/javascript">document.write(rnd)</script>

    questo il codice che stampa i numeri ,

    si puo fare cosi?

    <codice (primo numero)(numero estratto)> stampa il primo numero estratto
    <codice (secondo numero)(numero estratto)> stampa il secondo numero estratto
    ecc..

  10. #10
    Utente di HTML.it L'avatar di ganesha
    Registrato dal
    Jan 2003
    Messaggi
    357
    Ho messo il codice dentro una funzione che viene richiamata quanto la pagina è caricata.
    Riprova così

    codice:
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Documento senza titolo</title>
    <SCRIPT type="text/javascript">
    function numeri_random(){
    	var rnd=[];
    	for(var i=0;i<15;i++){ 
    		var tmp = Math.floor(Math.random()*90)+1;
    		var nuovo = true;
    		for(var j=0; j<i; j++){
    			if(rnd[j]==tmp)nuovo=false;
    		}
    		if(nuovo){
    			rnd[i]=tmp;
    		}else{
    			i--;
    		}
           }
       for(var i=0;i<3;i++){
          document.getElementById("_"+(i+1)).value = rnd[i]; 
       }
    }
    </SCRIPT>
    </head>
    
    <body onload="numeri_random();" >
    <table width="500" border="0">
      <tr>
        <td></td>
      </tr>
      <tr>
        <td><form id="numeri" name="numeri" method="post" action="">
          		<input name="_1" type="text" id="_1" />
                <input name="_2" type="text" id="_2" />
                <input name="_3" type="text" id="_3" />
        </form>
        </td>
      </tr>
    </table>
    </body>
    </html>

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 © 2025 vBulletin Solutions, Inc. All rights reserved.