SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Random Numbers




•http://www.cs.geneseo.edu/~baldwin/reference/random.html
Two Methods
By using Random Library Class
By using Math Library Class
RANDOM CLASS
Random Class
To use a class
  Import the class library
    import java.util.Random;
   OR
    import java.util.*;
  Create an object of class
    Random “name of object” = new Random()
    E.g
     • Random rand = new Random();
Generate a Random Number
          Integer
Have/Declare an integer variable
 int a;
Assign it a random value
 a=“object”.nextInt(“Value to define
 Range”);
 E.g
    a = rand.nextInt(100);
 If value is 100, range is 0 – 99
 Value will define the range as starting
 from 0 to one less than the value
Define Ranges, where A is an Integer
    and rand is Random Object
A=   rand.nextInt(100)      0 - 99
A=   rand.nextInt(10)       0-9
A=   rand.nextInt(101)      0 - 100
A=   rand.nextInt(99)       0 - 98
A=   rand.nextInt(100) +    0 – 99 and then add 5 to it =>
5                           5-104
A=   rand.nextInt(100) –    0 -99 and then subtract 10 =>
10                          -10 – 89
A=   rand.nextInt(10) + 1   0 – 9 and then add 1 => 1 - 10
                            0-100 and then add 5 => 5 - 105
A=   rand.nextInt(101)+5
                            0-9 and then subtract 10 => -10 – (-
A=   rand.nextInt(10) -10   1)
What if I have to generate a
Random Number from 0 –Random
                    Will generate a
                                      50,
                         Will generate a
       both inclusivethen0subtractfrom
                    Number from 0 – 99 first
                     random number
                     and        -49      50
                              fromWill generate random
                                  Will generate number
                                      generated a a
  A   =   rand.nextInt(50);  thus numberthe range50
                                    defines from 0 - as
                                random number from
                                 [0 – 49 and then will
                                 0 – 50] – [99 – 50] =
  A   =   rand.nextInt(51);             -50 - 49
                                  add 1 to it, makes
                                    range as 1 – 50
  A   =   rand.nextInt(100)-50;
  A   =   rand.nextInt(50)+1;
What if I have to generate a
Random Number from 5 – 15
      both inclusive?
a   =   rand.nextInt(15);
a   =   rand.nextInt(16);
a   =   rand.nextInt(10)+5;
a   =   rand.nextInt(11)+5;
Random Real Numbers
To generate a random real number
uniformly distributed between 0
and 1, use the "nextDouble"
message. This message takes no
parameters. For example...
double r = rand.nextDouble();
CLASS MATH
Math.random() call
double x = Math.random();
 Returns a double value with a positive
 sign, greater than or equal to 0.0 and
 less than 1.0.
 Will assign a value to x from [0.0 –
 0.999]
Generate Integer Random
        Number
Math.random()*10
 [0.0 – 9.99]
 Convert it into Integer by type casting
   (int)(Math.random()*10)
   [0 – 9]
Math.random()*100
 [0.0 – 99.99]
 Convert it into Integer by type casting
   (int)(Math.random()*100
   [0 – 99]

Weitere ähnliche Inhalte

Was ist angesagt?

Booths Multiplication Algorithm
Booths Multiplication AlgorithmBooths Multiplication Algorithm
Booths Multiplication Algorithmknightnick
 
Polynomial And Rational Funciotns 0921
Polynomial And Rational Funciotns 0921Polynomial And Rational Funciotns 0921
Polynomial And Rational Funciotns 0921ingroy
 
Monte-carlo sim pricing EU call
Monte-carlo sim pricing EU callMonte-carlo sim pricing EU call
Monte-carlo sim pricing EU callTianqi Huang, CFA
 
Formalization Machines and Sastes
Formalization Machines and SastesFormalization Machines and Sastes
Formalization Machines and SastesLahiru Danushka
 
PYTHON. AM CALL Pricing Trees
PYTHON. AM CALL Pricing TreesPYTHON. AM CALL Pricing Trees
PYTHON. AM CALL Pricing TreesTianqi Huang, CFA
 
9.3 Intro to Rational Functions
9.3 Intro to Rational Functions9.3 Intro to Rational Functions
9.3 Intro to Rational FunctionsKristen Fouss
 
Booth multiplication
Booth multiplicationBooth multiplication
Booth multiplicationM Madan Gopal
 
Machine learning
Machine learningMachine learning
Machine learningYShimizu2
 
Polynomial and thier graphs
Polynomial and thier graphsPolynomial and thier graphs
Polynomial and thier graphsJessica Garcia
 
Rational Functions
Rational FunctionsRational Functions
Rational FunctionsJazz0614
 
IB Maths.Turning points. Second derivative test
IB Maths.Turning points. Second derivative testIB Maths.Turning points. Second derivative test
IB Maths.Turning points. Second derivative testestelav
 
Random Number Generation
Random Number GenerationRandom Number Generation
Random Number GenerationRaj Bhatt
 
IB Maths. Turning points. First derivative test
IB Maths. Turning points. First derivative testIB Maths. Turning points. First derivative test
IB Maths. Turning points. First derivative testestelav
 
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygonsLiang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygonsLahiru Danushka
 
Tutorials--Graphs of Rational Functions
Tutorials--Graphs of Rational FunctionsTutorials--Graphs of Rational Functions
Tutorials--Graphs of Rational FunctionsMedia4math
 
If control structure in c lnaguage
If control structure in c lnaguageIf control structure in c lnaguage
If control structure in c lnaguageInnovative
 

Was ist angesagt? (19)

Booths Multiplication Algorithm
Booths Multiplication AlgorithmBooths Multiplication Algorithm
Booths Multiplication Algorithm
 
Polynomial And Rational Funciotns 0921
Polynomial And Rational Funciotns 0921Polynomial And Rational Funciotns 0921
Polynomial And Rational Funciotns 0921
 
Rational Function
Rational FunctionRational Function
Rational Function
 
Monte-carlo sim pricing EU call
Monte-carlo sim pricing EU callMonte-carlo sim pricing EU call
Monte-carlo sim pricing EU call
 
Formalization Machines and Sastes
Formalization Machines and SastesFormalization Machines and Sastes
Formalization Machines and Sastes
 
Booth Multiplier
Booth MultiplierBooth Multiplier
Booth Multiplier
 
PYTHON. AM CALL Pricing Trees
PYTHON. AM CALL Pricing TreesPYTHON. AM CALL Pricing Trees
PYTHON. AM CALL Pricing Trees
 
9.3 Intro to Rational Functions
9.3 Intro to Rational Functions9.3 Intro to Rational Functions
9.3 Intro to Rational Functions
 
Booth multiplication
Booth multiplicationBooth multiplication
Booth multiplication
 
Machine learning
Machine learningMachine learning
Machine learning
 
Polynomial and thier graphs
Polynomial and thier graphsPolynomial and thier graphs
Polynomial and thier graphs
 
Rational Functions
Rational FunctionsRational Functions
Rational Functions
 
Cg
CgCg
Cg
 
IB Maths.Turning points. Second derivative test
IB Maths.Turning points. Second derivative testIB Maths.Turning points. Second derivative test
IB Maths.Turning points. Second derivative test
 
Random Number Generation
Random Number GenerationRandom Number Generation
Random Number Generation
 
IB Maths. Turning points. First derivative test
IB Maths. Turning points. First derivative testIB Maths. Turning points. First derivative test
IB Maths. Turning points. First derivative test
 
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygonsLiang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
 
Tutorials--Graphs of Rational Functions
Tutorials--Graphs of Rational FunctionsTutorials--Graphs of Rational Functions
Tutorials--Graphs of Rational Functions
 
If control structure in c lnaguage
If control structure in c lnaguageIf control structure in c lnaguage
If control structure in c lnaguage
 

Andere mochten auch

Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Chloe Main
 

Andere mochten auch (8)

Comp102 lec 3
Comp102   lec 3Comp102   lec 3
Comp102 lec 3
 
Comp102 lec 10
Comp102   lec 10Comp102   lec 10
Comp102 lec 10
 
Comp102 lec 4
Comp102   lec 4Comp102   lec 4
Comp102 lec 4
 
Comp102 lec 9
Comp102   lec 9Comp102   lec 9
Comp102 lec 9
 
Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!
 
Comp102 lec 7
Comp102   lec 7Comp102   lec 7
Comp102 lec 7
 
Comp102 lec 8
Comp102   lec 8Comp102   lec 8
Comp102 lec 8
 
Comp102 lec 5.1
Comp102   lec 5.1Comp102   lec 5.1
Comp102 lec 5.1
 

Comp102 lec 5.0

  • 2. Two Methods By using Random Library Class By using Math Library Class
  • 4. Random Class To use a class Import the class library import java.util.Random; OR import java.util.*; Create an object of class Random “name of object” = new Random() E.g • Random rand = new Random();
  • 5. Generate a Random Number Integer Have/Declare an integer variable int a; Assign it a random value a=“object”.nextInt(“Value to define Range”); E.g a = rand.nextInt(100); If value is 100, range is 0 – 99 Value will define the range as starting from 0 to one less than the value
  • 6. Define Ranges, where A is an Integer and rand is Random Object A= rand.nextInt(100) 0 - 99 A= rand.nextInt(10) 0-9 A= rand.nextInt(101) 0 - 100 A= rand.nextInt(99) 0 - 98 A= rand.nextInt(100) + 0 – 99 and then add 5 to it => 5 5-104 A= rand.nextInt(100) – 0 -99 and then subtract 10 => 10 -10 – 89 A= rand.nextInt(10) + 1 0 – 9 and then add 1 => 1 - 10 0-100 and then add 5 => 5 - 105 A= rand.nextInt(101)+5 0-9 and then subtract 10 => -10 – (- A= rand.nextInt(10) -10 1)
  • 7. What if I have to generate a Random Number from 0 –Random Will generate a 50, Will generate a both inclusivethen0subtractfrom Number from 0 – 99 first random number and -49 50 fromWill generate random Will generate number generated a a A = rand.nextInt(50); thus numberthe range50 defines from 0 - as random number from [0 – 49 and then will 0 – 50] – [99 – 50] = A = rand.nextInt(51); -50 - 49 add 1 to it, makes range as 1 – 50 A = rand.nextInt(100)-50; A = rand.nextInt(50)+1;
  • 8. What if I have to generate a Random Number from 5 – 15 both inclusive? a = rand.nextInt(15); a = rand.nextInt(16); a = rand.nextInt(10)+5; a = rand.nextInt(11)+5;
  • 9. Random Real Numbers To generate a random real number uniformly distributed between 0 and 1, use the "nextDouble" message. This message takes no parameters. For example... double r = rand.nextDouble();
  • 11. Math.random() call double x = Math.random(); Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Will assign a value to x from [0.0 – 0.999]
  • 12. Generate Integer Random Number Math.random()*10 [0.0 – 9.99] Convert it into Integer by type casting (int)(Math.random()*10) [0 – 9] Math.random()*100 [0.0 – 99.99] Convert it into Integer by type casting (int)(Math.random()*100 [0 – 99]