Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

random integer example

Posted on 2014-01-31
10
Medium Priority
?
374 Views
Last Modified: 2014-02-13
Hi,

I am running below example

http://www.avajava.com/tutorials/lessons/how-do-i-generate-a-random-int-in-java.html

i got output as
random integer:-2143199807
random integer:128861668
random integer:1688911854
random integer:-1021991149
random integer:69006493
random integer:110974251
random integer:846899949
random integer:1052181148
random integer:-1013327507
random integer:1169652165

random integer from 1 to 5:5
random integer from 1 to 5:1
random integer from 1 to 5:1
random integer from 1 to 5:5
random integer from 1 to 5:2
random integer from 1 to 5:2
random integer from 1 to 5:4
random integer from 1 to 5:5
random integer from 1 to 5:5
random integer from 1 to 5:4

I wonder what is the meaning of above output. Esp like 5:6 etc.

What are the practical uses, purpose of random integer values.

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 39825545
I wonder what is the meaning of above output. Esp like 5:6 etc.
There is no 6. The output means that the value in the range 1 to 5 is :n

e.g.

random integer from 1 to 5:2

Open in new window


the random value generated in that range was 2
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 400 total points
ID: 39825631
What are the practical uses, purpose of random integer values.

There are many, from cryptography to graphics, to you name it. As a trivial example, say you wanted to have a little coloured control - textbox or label - on your GUI, and you wanted the text in that label to change its font colour for some reason every few seconds - perhaps to show the user that the app was still running, or to give the interface a bit of a facelift and make it more 'interesting'.

Without a random number generator, you would have to code the colours and the sequence for the display of the text in the colour you had coded for. But if you just wanted to change the colours randomly, you could use the Random generator to 'decide' the colour for you, and then apply it to the text. I'm sure there are better uses for Random, but this one came to mind for some reason. ;)
0
 
LVL 32

Expert Comment

by:awking00
ID: 39830043
The Random.nextint() method produces a random number that is between -2,147,483,648 and 2,147,483,647 (the minimum and maximum values of an int). The Random.nextint(int n) method produces a random number between 0 (inclusive) and n (exclusive), so if high is 5, Random.nextint(5) produces a number between 0 and 4 and when added to the low of 1, becomes a number between 1 and 5. As for the use of random integers, they can have many applications. They can be used in games of chance, they can be used to obfuscate values so they're not meaningful to the user (e.g. disguising social security numbers), and many others.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 37

Expert Comment

by:zzynx
ID: 39831752
>> What are the practical uses, purpose of random integer values.
A typical real world example is: the rolling of a dice. (it generates a number between 1 and 6)

You can use it everywhere you need "pseudo real data".
Eg. You want to draw a XY-graph where the data on the Y-axis can be between 0 an 20.
If you want a "real looking" graph, the best thing to do is using random values.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39842910
// generates a random int
            for (int i = 0; i < 10; i++) {
                  anyRandomInt(random);
            }

            System.out.println();

            // generates a random int in a range from low int to high int
            for (int i = 0; i < 10; i++) {
                  anyRandomIntRange(random, 1, 5);
            }


what is the difference between above For loops.

Why author put loop i<10.

where is the range defined in the program and what is the use of Range?

please advise
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 400 total points
ID: 39843198
In the first loop 10 random integers are generated. (that is between -2,147,483,648 and 2,147,483,647)

In the second loop 10 random integers are generated too. But this time they are between 1 and 5 as the comment says.
The two parameters 1 and 5 form the range.

If you call anyRandomInt() with one parameter it generates an integer in the range [-2,147,483,648 : 2,147,483,647]
If you call it with two extra parameters it generates an integer in the range [parameter 2 : parameter 3]
The first parameter is the variable to put the generated integer in.

I hope that is clear.
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 400 total points
ID: 39847282
>>what is the difference between above For loops.<<
There have been two methods created, anyRandomInt and anyRandomIntRange, both of which use the Java Random.nextInt method. One without a parameter, Random.nextInt(), and one with an int parameter, Random.nextInt(int high) and they perform just as I described in ID: 39830043.

>>Why author put loop i<10. <<
Because he/she only wanted to generate 10 random numbers.

<<where is the range defined in the program and what is the use of Range?<<
The range is defined by using the Random.nextInt(int parameter) method which, as I also described creates a random integer from 0 up to, but not including the value of the parameter (in your example, 5). The anyRandomIntRange method then adds the generated random int to the low value (in your example 1) to create the range 1 to 5. The use of the range is to limit the values than can be returned. For example, the game of Keno randomly selects 20 numbers out of the possible values of 1 to 80. So to emulate that, you would use something like -
Random rand = new Random();
for (int i=0; i<20,i++)
int numberSelected = rand.nextInt(80) + 1;
system.out.println(numberSelected);

Note - in reality you would need to add code so as not to generate duplicates. Hope this helps.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39854793
int randomInt = random.nextInt(high) + low;

what is happening in above line.

why above loop produces big numbers as below

random integer:-2143199807
random integer:128861668
random integer:1688911854
random integer:-1021991149
random integer:69006493
random integer:110974251
random integer:846899949
random integer:1052181148
random integer:-1013327507
random integer:1169652165




where as below loop produces like 5,1,1,5,2 etc as below
random integer from 1 to 5:5
random integer from 1 to 5:1
random integer from 1 to 5:1
random integer from 1 to 5:5
random integer from 1 to 5:2
random integer from 1 to 5:2
random integer from 1 to 5:4
random integer from 1 to 5:5
random integer from 1 to 5:5
random integer from 1 to 5:4





what is difference between below two lines below
random.nextInt(high)

random.nextInt();
0
 
LVL 36

Accepted Solution

by:
mccarl earned 400 total points
ID: 39854884
what is difference between below two lines below

random.nextInt(high)

random.nextInt();
The javadoc for Random is pretty clear on this...



nextInt(high)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive) ...
(Note the inclusive and exclusive in the above quote). So if for example 'high' = 8, it returns a random number that can have any value from the following... 0, 1, 2, 3, 4, 5, 6, or 7



nextInt()
Returns the next pseudorandom, uniformly distributed int value  ...  All 2^32 possible int values are produced with (approximately) equal probability
So this returns a random number in the full range that an int can represent,
ie. between -2147483648 and 2147483647
0
 
LVL 7

Author Comment

by:gudii9
ID: 39857309
turns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive) ...

since specified max value in the program 5 is exclusing eventhough we are adding low(1) to the
nextInt we only see upto 5 only(ie 5,1,1,5,2) and no 6
int randomInt = random.nextInt(high) + low;
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question