17ETCS002072 Harshit Agarwal

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Name 

– Harshit Agarwal    Registration Number – 17ETCS002072 

Laboratory 1 
Title of the Laboratory Exercise:   Introduction to java simulation 
1. Introduction and Purpose of Experiment 
Computer  simulation  provides  students  to  design  and  implement  computer  simulation  models, 
conduct simulation experiments and evaluate system performance. This laboratory exercise will help 
the students to get familiar with using object‐oriented simulation in Java. 
  Java (Structured Parallel Discrete Event Simulation in Java) system is designed to incorporate 
the  parallel  programming  technology  into  discrete  event  simulations.  The  java  system  adopts  the 
approach of augmenting a general‐purpose language with essential constructs to support simulation 
modeling based on the process‐oriented modeling technology 
2. Aim and Objectives 
Aim 
 To use Netbeans and understand using object‐oriented simulation in Java   
Objectives 
At the end of this lab, the student will be able to 
 Explain the features and use of Netbeans IDE to develop java programs for simulation 
 Edit, compile and execute java programs successfully using Netbeans IDE 
3. Experimental Procedure 
Students  are  given  a  set  of  programs  for  generating  random  numbers  using  built‐in  methods. 
Programs should be edited, compiled and executed using Netbeans IDE. 
  Random number generation using inbuilt methods/manually 
  Ex: coin toss, die, and cards 
 
 
 
 
 
 
 
 
 
Name – Harshit Agarwal    Registration Number – 17ETCS002072 

4. Calculations/Computations/Algorithms 
a. Generate a 10 Random Number 
begin 
   rand := new Random() 
   for i ∈ {0,....,9} do 
     num := write rand.nextInt()  
     write num  
   end for 
 end 
b. Generate 10 Random number between 0 and 1 
begin 
     rand := new Random() 
    for i ∈ {0,....,9} do 
      num := write rand.nextFloat() 
      write num   
    end for 
end 
c. Generate 10 Distributed Random number 
  begin 
     rand := new Random() 
     for i ∈ {0,....,9} do 
      num := write rand.nextGaussian() * 𝜎 + 𝑥̅  
      write num   
    end for 
  end 
d. Generate the probability of the dice rolled 100 times randomly 
  begin 
     var num[100]:int 
    var count := 0,sum := 0:float 
    rand := new Random() 
     for i ∈ {1,....,10} do 
      num[i] := write rand.nextInt() + 1 in range 0:6   
    end for 
    for i ∈ {1,....,6} do 
      for j ∈ {0,....,100} do 
        if num[i] = j then 
          count++ 
        end if 
      end for 
      write count / 100 
      initialize count := 0 
    end for 
  end 
5. Presentation of Results 
Code Implementation ‐‐  

a. Generate a 10 Random Number 
Name – Harshit Agarwal    Registration Number – 17ETCS002072 

package cslab1a; 
 
import java.util.*; 
 
public class CsLab1a { 
 
    public static void main(String[] args) { 
        int i; 
        Random rand = new Random(); 
        for (i = 0; i < 10; i++) { 
            int num = rand.nextInt(500); 
            System.out.println(i + 1 + " The Random Number are :" + num); 
        } 
    } 

b. Generate 10 Random number between 0 and 1 
package cslab1b; 
 
import java.util.*; 
 
public class CsLab1b { 
 
    public static void main(String[] args) { 
        int i; 
        Random rand = new Random(); 
        for (i = 0; i < 10; i++) { 
            float num = rand.nextFloat(); 
            System.out.println(i + 1 + " The Random Number are :" + num); 
        } 
    } 

c. Generate 10 Distributed Random number 
package cslab1c; 
 
import java.util.*; 
 
public class CsLab1c { 
 
    public static void main(String[] args) { 
        int i; 
        Random rand = new Random(); 
        for (i = 0; i < 10; i++) { 
            float num = (float) (rand.nextGaussian() * 0.1 + 0.5); 
            System.out.println(i + 1 + " The Random Number are :" + num); 
        } 
    } 

 
 
 
 
 
Name – Harshit Agarwal    Registration Number – 17ETCS002072 

d. Generate the probability of the dice rolled 100 times randomly 
package cslab1d; 
import java.util.*; 
public class CsLab1d { 
 
    public static void main(String[] args) { 
        int i; 
        int[] num = new int[100]; 
        float count = 0, sum = 0; 
        Random rand = new Random(); 
        for (i = 0; i < 100; i++) { 
            num[i] = rand.nextInt(6) + 1; 
        } 
        for (int j = 1; j <= 6; j++) { 
            for (i = 0; i < 100; i++) { 
                if (num[i] == j) { 
                    count++; 
                } 
            } 
            System.out.println("The probability of " + (j) + " is " + (count / 
100)); 
            sum = sum + (count / 100); 
            count = 0; 
        } 
    } 

Code Execution ‐‐  
e. Generate a 10 Random Number 

 
f. Generate 10 Random number between 0 and 1 

 
Name – Harshit Agarwal    Registration Number – 17ETCS002072 

g. Generate 10 Distributed Random number 

 
h. Generate the probability of the dice rolled 100 times randomly 

 
6. Analysis and Discussions 
In  Java,  Random  Number  can  be  obtained  simply  by  using  the 
java.util.Random  class. The random numbers to be generated need to be 

from  a  certain  range  for  which,  the  nextInt(),  nextFloat()  & 


nextGaussian()  method can also accept an int parameter, float parameter 

and parameters to generate distributed Random Numbers. The Random class 
generates random numbers in a deterministic way.  
7. Conclusions  
Random class is part of java.util package. An instance of java Random class 
is used to generate random numbers. This class provides several methods to 
generate random numbers of type integer, double, long, float etc. Random 
number generation algorithm works on the seed value. If not provided, seed 
value is created from system nano time. 
8. Comments  
  1. Limitations of Experiments 
We have to change the data type of the generating random numbers if we work with decimals. 
  2. Limitations of Results 
If the seed value is same the same set of Random Number will be generated always. 
  3. Learning happened 
Throughout  this  Laboratory  we  got  a  basic  idea  of  how  to  use  and  where  to  use  to  generate  random 
numbers for any future simulating environment. 
  4. Recommendations 
Using Math.random() would likely avoid frequent changes in data type. 

You might also like