Math.random() problems in method

user2935864

this might not be the hardest thing to acheive, yet I'm still having problems :S:

In my little program I'm simulating a card game (http://tinyurl.com/pf9fhf4) and I need to generate a random number from the range [0,35] in increments of 5. Ergo, the possible values should be : 0, 5, 10, 15, 20, 25, 30, 35. I've tried this in a sepparate class first like this:

class RandomValue {
public static void main (String [] args){

int i =0;

    do {
    int n = (int) (Math.random()*36 );
        if (n%5 ==0){
        System.out.println(n);
        i++;
        }

    } while (i <1); 
  }
}

And this works!!!

When I tried making a method that would return this generated value :

public class Tarot {

public static int rValue (){

int i =0;

    do {
    int n = (int) (Math.random()*36 );
        if (n%5 ==0){
        int r =n;
        i++;
        }       
    }while(i<1);
    return r;   
  }
}

it returns an error:

Tarok.java:14: error: cannot find symbol
            return r;
                   ^

What am I doing wrong, any suggestions how to do this in a more pretty way?

SpringLearner

change this code

public class Tarot {

public static int rValue (){

int i =0;

    do {
    int n = (int) (Math.random()*36 );
        if (n%5 ==0){
        int r =n;
        i++;
        }       
    }while(i<1);
    return r;   
  }
}

to

public class Tarot {

public static int rValue (){

int i =0;
int r =0
    do {
    int n = (int) (Math.random()*36 );
        if (n%5 ==0){
        r=n;
        i++;
        }       
    }while(i<1);
    return r;   
  }
}

reason

The scope of variable r is within if loop So when trying to return r compiler did not find r

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

StackOverflowError in Math.Random in a randomly recursive method

From Dev

how to create a java function that makes random math problems.

From Dev

using math.random method to create dice method

From Dev

In Java 8 why we cannot convert Math.random() to Math::random using method references

From Dev

Getting same number when using Math.random() and ThreadLocalRandom.current().nextDouble() and Random class nextDouble() method?

From Dev

Is it possible for Math.random() === Math.random()

From Dev

Javascript Math.random() not *that* random…?

From Dev

Math.Random is not so random

From Dev

Math.Round problems

From Dev

Math.random not working

From Dev

Math & JavaScript | Random chances

From Dev

Arrays Random Multiplication Math

From Dev

Math.random Scenario

From Dev

How to compare Math.random() method generated current value with previous value

From Dev

What do I need to do to get my Java code to recognize the method Math.random()?

From Dev

Swift random number problems

From Dev

Random HTTP connection problems

From Dev

Problems with random images in array

From Dev

Problems with random numbers (C)

From Dev

Async Await Loop/Math problems

From Dev

Math application to calculate advance problems

From Dev

problems with compiling openMP and math library

From Dev

list/math problems returns zero

From Dev

Bash: Date/Time math problems

From Dev

Java Math.random() How random is it?

From Dev

Math.random number of random bits

From Dev

Java Math.random not random in while loop

From Dev

Math.Random() To pick random array

From Dev

Making Math.random() more random?