• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

random integer example

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
gudii9
Asked:
gudii9
  • 3
  • 2
  • 2
  • +3
5 Solutions
 
CEHJCommented:
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
 
krakatoaCommented:
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
 
awking00Commented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
zzynxSoftware engineerCommented:
>> 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
 
gudii9Author Commented:
// 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
 
zzynxSoftware engineerCommented:
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
 
awking00Commented:
>>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
 
gudii9Author Commented:
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
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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
 
gudii9Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now