Como gero um numero randomico?

Ola estou tentando assim:

x = 10 + (int) ( Math.random() * 99 );

onde o x eh: int x;

porem da o erro:

cannot find symbol
symbol: variable Random

como, gero um numero randomico?

[]s

dê uma olhada nesse artigo:

http://www.guj.com.br/java.artigo.17.1.guj

:wink:

cara usei o mesmo exemplo que vc postou e ele esta gerando blz! verifica se vc não escreveu random() com o R maiusculo!

vc colocou na hora que postou o erro o R maiusculo! as vezes pode ser isto!

[]s Victor …

amigos, eis o que me aconteceu!

tentei com o R maiusculo e continua dando erro…

agora tentei do jeito que o amigo disse, e funcionou! porem da outro erro!

 n = new int[10];
           
            x = random.nextInt(99);

                  
            n[1] = random.nextInt(10);
            n[3] = random.nextInt(10);
            n[5] = random.nextInt(10);
            n[7] = random.nextInt(10);
            n[9] = random.nextInt(10);
                
            n[2] = random.nextInt(10);
            n[4] = random.nextInt(10);
            n[6] = random.nextInt(10);
            n[8] = random.nextInt(10);
            
            sum1 = n[1] + n[3] + n[5] + n[7] + n[9];
            sum2 = n[2] + n[4] + n[6] + n[8];

            s = x - sum1;
            n[10] = s - sum2;

Todas as variaveis criadas como int

nesta parte
s = x - sum1;
n[10] = s - sum2;

da o erro:

incompatible types
found: int
required: javax.microedition.lcdui.Displayable

bem… o que esta acontecendo agora?^

um pergunta

        x = random.nextInt(99);

pode gerar qualquer numero de 0 a 99, porem queria que fosse de 10 a 99, como faco?

ah… o n eh um vetor de int

[]s

da pra fazer assim

[code]
int x = random.nextInt(99);

while (x<10) {
x = random.nextInt(99);
}

tenta ai

[]'s[/code]

Valeu galera… funcionou perfeitamente, agora não estou conseguindo transformar o n[i] (inteiro) pra string… :frowning:

[]s

String string = String.valueOf&#40; n&#91;i&#93; &#41;;

assim da certo!!

[]'s

valeu, funcinou, porem deu outro erro :smiley:

/*
 * HelloMIDlet.java
 *
 * Created on 2 de Março de 2005, 15&#58;55
 */

package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;

/**
 * An example MIDlet with simple &quot;Hello&quot; text and an Exit command.
 * Refer to the startApp, pauseApp, and destroyApp
 * methods so see how each handles the requested transition.
 *
 * @author  Felipe
 * @version
 */
public class HelloMIDlet extends MIDlet implements CommandListener &#123;
    
    private Command exitCommand, gerarCommand; // The exit command
    private Display display;    // The display for this MIDlet
    int n&#91;&#93;;
    Random random = new Random&#40;19857&#41;;
    int sum1, sum2;
    String strchave;
            
           
    public HelloMIDlet&#40;&#41; &#123;
        display = Display.getDisplay&#40;this&#41;;
        exitCommand = new Command&#40;&quot;Sair&quot;, Command.SCREEN, 2&#41;;
        gerarCommand = new Command&#40;&quot;Gerar&quot;, Command.SCREEN, 3&#41;;
    &#125;
    
    /**
     * Start up the Hello MIDlet by creating the TextBox and associating
     * the exit command and listener.
     */
    public void startApp&#40;&#41; &#123;
        TextBox t = new TextBox&#40;&quot;Gerador de Seriais &quot;, &quot;String de Teste&quot;, 256, 0&#41;;
        
        t.addCommand&#40;exitCommand&#41;;
        t.addCommand&#40;gerarCommand&#41;;
        t.setCommandListener&#40;this&#41;;
        
        display.setCurrent&#40;t&#41;;
    &#125;
    
    /**
     * Pause is a no-op since there are no background activities or
     * record stores that need to be closed.
     */
    public void pauseApp&#40;&#41; &#123;
    &#125;
    
    /**
     * Destroy must cleanup everything not handled by the garbage collector.
     * In this case there is nothing to cleanup.
     */
    public void destroyApp&#40;boolean unconditional&#41; &#123;
    &#125;
    
    /*
     * Respond to commands, including exit
     * On the exit command, cleanup and notify that the MIDlet has been destroyed.
     */
    public void commandAction&#40;Command c, Displayable s&#41; &#123;
        if &#40;c == exitCommand&#41; &#123;
            destroyApp&#40;false&#41;;
            notifyDestroyed&#40;&#41;;
        &#125;

        if &#40;c == gerarCommand&#41; &#123;
            n = new int&#91;10&#93;;
                
            n&#91;1&#93; = random.nextInt&#40;10&#41;;
            n&#91;3&#93; = random.nextInt&#40;10&#41;;
            n&#91;5&#93; = random.nextInt&#40;10&#41;;
            n&#91;7&#93; = random.nextInt&#40;10&#41;;
            n&#91;9&#93; = random.nextInt&#40;10&#41;;
                
            n&#91;2&#93; = random.nextInt&#40;10&#41;;
            n&#91;4&#93; = random.nextInt&#40;10&#41;;
            n&#91;6&#93; = random.nextInt&#40;10&#41;;
            n&#91;8&#93; = random.nextInt&#40;10&#41;;
            
            sum1 = n&#91;1&#93; + n&#91;3&#93; + n&#91;5&#93; + n&#91;7&#93; + n&#91;9&#93;;
            sum2 = n&#91;2&#93; + n&#91;4&#93; + n&#91;6&#93; + n&#91;8&#93;;

            int x = random.nextInt&#40;99&#41;; 

            while &#40;x&lt;10&#41; &#123; 
                x = random.nextInt&#40;99&#41;; 
            &#125; 
            
           int y = x - sum1;
           
           n&#91;10&#93; = y - sum2;
           
           Alert alert = new Alert&#40;&quot;Chave de Liberacao&quot;&#41;;
           alert.setTimeout&#40;Alert.FOREVER&#41;;
           
           strchave = 'C' + String.valueOf&#40;n&#91;1&#93;&#41; + String.valueOf&#40;n&#91;2&#93;&#41; +
           String.valueOf&#40;n&#91;3&#93;&#41; + String.valueOf&#40;n&#91;4&#93;&#41; + String.valueOf&#40;n&#91;5&#93;&#41; + String.valueOf&#40;n&#91;6&#93;&#41;
           + String.valueOf&#40;n&#91;7&#93;&#41; + String.valueOf&#40;n&#91;8&#93;&#41; + String.valueOf&#40;n&#91;9&#93;&#41; + String.valueOf&#40;n&#91;10&#93;&#41;
           + String.valueOf&#40;x&#41;;
           
           alert.setString&#40;strchave&#41;;
                   
        &#125;
    &#125;
    
&#125;

Erro:

java.lang.ArrayIndexOutOfBoundsException at hello.HelloMIDlet.commandAction&#40;+295&#41; at javax.microedition.lcdui.Display$DisplayAccessor.commandAction&#40;+282&#41; at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction&#40;+10&#41; at com.sun.midp.lcdui.DefaultEventHandler.commandEvent&#40;+68&#41; at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent&#40;+47&#41; at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run&#40;+250&#41; Execution completed.

Cara, desculpe, devo estar enchendo o saco, mas nao estou conseguindo… :frowning:

[]s

hee nem esquenta no comeco eh foda mesmo

entao tem essa linha q vc faz isso :

  • String.valueOf(n[10])

o n eh um vetor de no maximo 10!!!
entao ele vai da posicao 0 ate a posicao 9

na linha q eu mostrei acima vc ta tentando acessar a posicao 10…que nao existe!!
é isso

[]'s!!!

se vc tiver icq meu numero eh 26756914

falow

Olá pessoal entro atrasado na conversa mas só queria acrescentar uma coisa
O método

Só existe em dispositivos que implementam CLDC 1.1 e não 1.0.
Por exemplo os celulares da siemens só os da série 60 em diante
Ok
ps.: para ver uma lista e qual a versão acesse

http://developers.sun.com/techtopics/mobility/device/device