Solved

random integer example

Posted on 2014-01-31
10
353 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
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 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 100 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 31

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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 100 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 31

Assisted Solution

by:awking00
awking00 earned 100 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 35

Accepted Solution

by:
mccarl earned 100 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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now