Random Generator - carrying over variables

random number generator
random number generator algorithm
random number generator no repeats
random number generator python
random number generator c++
rng
pseudo random
random numbers generator

Would anyone please be able to help? I am just starting out with some very basic android development. I am developing an initial app to help my daughter learn her times tables. Idea being (in its first version) randomly generate 2 numbers between 1 and 12. Then click on a calculate button which will give the answer. I have it so that it randomly generates the 2 numbers, however, when I click calculate, it does not calculate, simply shows 0. I believe it is something to do with the value of the variables (digit1 & digit2) not being seen by the next method (calculate). If I hard code 2 numbers into the calculate method, it works. I just cannot the random generated numbers to calculate. Any help would be most appreciated. MainActivity.java:

package uk.co.myrayner.sophiestimestables;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.util.Random;


public class MainActivity extends AppCompatActivity {

    int digit1;
    int digit2;
    int show_answer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void randomise_digits(View view) {
        Random rand1 = new Random();
        int digit1 = rand1.nextInt(12) + 1;
        displaydigit1(digit1);
        Random rand2 = new Random();
        int digit2 = rand2.nextInt(12) + 1;
        displaydigit2(digit2);
    }

    public void calculate(View view) {
        show_answer = (digit1 * digit2);
        displayanswer(show_answer);
    }

    private void displaydigit1(int number) {
        TextView digit1TextView = (TextView) findViewById(R.id.digit1);
        digit1TextView.setText("" + number);
    }

    private void displaydigit2(int number) {
        TextView digit2TextView = (TextView) findViewById(R.id.digit2);
        digit2TextView.setText("" + number);
    }

    private void displayanswer(int number) {
        TextView answerTextView = (TextView) findViewById(R.id.show_answer);
        answerTextView.setText("" + number);

    }
}

All you need to do is remove the "int" before you define digit1 and digit2 so your code should look like this:

package uk.co.myrayner.sophiestimestables;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    int digit1;
    int digit2;
    int show_answer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void randomise_digits(View view) {
        Random rand1 = new Random();
        digit1 = rand1.nextInt(12) + 1;
        displaydigit1(digit1);
        Random rand2 = new Random();
        digit2 = rand2.nextInt(12) + 1;
        displaydigit2(digit2);
    }

    public void calculate(View view) {
        show_answer = (digit1 * digit2);
        displayanswer(show_answer);
    }

    private void displaydigit1(int number) {
        TextView digit1TextView = (TextView) findViewById(R.id.digit1);
        digit1TextView.setText("" + number);
    }

    private void displaydigit2(int number) {
        TextView digit2TextView = (TextView) findViewById(R.id.digit2);
        digit2TextView.setText("" + number);
    }

    private void displayanswer(int number) {
        TextView answerTextView = (TextView) findViewById(R.id.show_answer);
        answerTextView.setText("" + number);

    }
}

Six Fundamental Methods to Generate a Random Variable – Win , As we will see: complexity goes up as we move through the list (so at some has evolved from cobbled together procedures passing ad-hoc tests (such as The most basic form of a sequential pseudo random generator is a  Generating Random Variables Christian Robert Université Paris Dauphine and CREST, INSEE and George Casella University of Florida Keywords and Phrases: Random Number Generator, Probability Integral Transform, Accept Reject Algorithm, Metropolis-Hastings Algorithm, Gibbs Sampling, Markov Chains

There are several problems with your implementation.

1) Inside your randomise_digits you are setting the values of digit1 and digit2, but those are local variables, not the instance variables declared outside of the method. So any changes to digit1 and digit2 inside randomise_digits() is not reflected on the digit1 and digit2 of your MainActivity object. That is why you are getting 0 because they are not actaully set to any value (Since you refer to the instance variables of digit1 and digit2 inside your calculate() method). The local digit1 and digit2 in your randomise_digits() method is now out of scope/does not exist anymore.

2) Change the name of your method to better reflect what they are doing. Your calculate() method should probably renamed to show(). You don't need to pass in View objects as parameters for all of your methods. You are not using them so why include them?

3) You do not need to create two random objects to calculate two random numbers. One will do just fine.

Your class should probably look something like this:

package uk.co.myrayner.sophiestimestables;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.util.Random;


public class MainActivity extends AppCompatActivity {

    int digit1;
    int digit2;
    int show_answer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button calculateButton = findViewById(R.id.your_calculate_button_id);

        calculateButton.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                randomise_digits();
                calculate();
            }

        });
    }

    public void randomise_digits() {
        Random rand1 = new Random();
        digit1 = rand1.nextInt(12) + 1;
        displaydigit1(digit1);
        digit2 = rand1.nextInt(12) + 1;
        displaydigit2(digit2);
    }

    public void calculate() {
        show_answer = (digit1 * digit2);
        displayanswer(show_answer);
    }

    private void displaydigit1(int number) {
        TextView digit1TextView = (TextView) findViewById(R.id.digit1);
        digit1TextView.setText("" + number);
    }

    private void displaydigit2(int number) {
        TextView digit2TextView = (TextView) findViewById(R.id.digit2);
        digit2TextView.setText("" + number);
    }

    private void displayanswer(int number) {
        TextView answerTextView = (TextView) findViewById(R.id.show_answer);
        answerTextView.setText("" + number);

    }
}

Make sure to remove any onclick events in your activity's layout xml if you decide to copy and paste the code above.

Programming, What we want is a repeatable sequence of seemingly random numbers that satisfy Note that "Random" number generators produce what are called "​pseudo" random We don't really need a next and previous variables, so we can say:. A linear congruential generator (LCG) represents one of the oldest and best-known pseudorandom number generator algorithms. The generator is defined by the recurrence relation: X n+1 = (a X n + c) modulo mwhere . X. n. is the sequence of pseudorandom values, and. The period of a general LCG is at most . m, and for some choices of . a. much less

I didn't test your code in my machine. But I think it may be caused by that you haven't set value of your global variables 'digit1' and 'digit2'. In your method 'randomise_digits', you just create new local variables 'digit1' and 'digit2'.

[PDF] 13 Random Variables and Simulation, Random variables play two important roles in simulation models. We assume that As we will see below, both cases rely on the random number generator. be approximately uniformly distributed over (0,1). In 1951 not cause overflow—​intermediate calculations may need to be carried out using extended precision  @Anson: The reason that Johannes Rudolph is complaining is that % does not work unless the value on the right is an exact divisor of RAND_MAX. If it is not then some values have a slight (very slight) smaller probability. example: RAND_MAX (32767) r = rand()%3; Probability of 0(10923/32767) 1(10922/32767) 2(10922/32767) Notice the probability of a 0 is slightly higher.

Well, I didn't test your code but apparently, you declared digit1 and digit2 as global variables but when you use it, you declared it again so now the code will generate the random number and assign to the local digit1 and digit2 and not the global and when you try to call calculate(), global digit1 and digit2 are still 0 (primitive integer is zero by default) so basically the result will be zero.

Just modify randromise_digits function to

public void randomise_digits(View view) {
    Random rand1 = new Random();
    digit1 = rand1.nextInt(12) + 1;
    displaydigit1(digit1);
    Random rand2 = new Random();
    digit2 = rand2.nextInt(12) + 1;
    displaydigit2(digit2);
}

Local Structure from Diffraction, The correlations are plotted against the number of lattice repeats, since the correlation field is containing prescribed occupancy correlations can be carried over quite In contrast to the variables in Equ. is a continuous variable and it is  random number generator to two different variables. Learn more about random numbers

International Handbook of Earthquake & Engineering Seismology, Part B, The major statistical packages now have excellent random number of independent random variables do not require great changes to carry over to the general  Let's say that I have a categorical variable which can take the values A, B, C and D. How can I generate 10000 random data points and control for the frequency of each? For example: A = 10% B = 20

Performance Evaluation of Computer and Communication Systems, 6.9 Review 6.1 How do you generate a sample of a real random variable with the seed of the random generator can be carried over from one run to the next. Random Variables many a times confused with traditional variables. In this blog post we will see what random variables are and why do we need them over traditional variables. In probability and statistics, random variable, random quantity or stochastic variable is a variable whose possible values are the outcomes of a random phenomenon— Wikipedia

Random number generation, A random number generator (RNG) is a device that generates a sequence of numbers or as in security applications, hardware generators are generally preferred over pseudo-random algorithms, where feasible. can be used to generate random numbers of any desired distribution by passing them through the inverse  In addition to its abstract members, the Distribution type provides some implementation details common to all random number distributions. As the computation of distributed random numbers necessarily requires a random number generator, the generator field stores an instance of the Generator class.

Comments
  • calculate() is not generating random numbers, randomise_digits is generating the numbers. Check if the click handlers are setup correctly
  • Thank you so much, have got it working by removing the int from within the randomise_digits method.