AyanUpadhaya

Custom Random Class For Random Int and Random Item in Javascript

Aug 2nd, 2021 (edited)
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // My custom random random class for generating random integer number
  2.  
  3. // and randomly selecting item from list or array
  4.  
  5. // inspired by Python
  6.  
  7. class Random{
  8.     constructor(list=null){
  9.         this.list=list;
  10.     }
  11.     choice(){
  12.         let num = Math.random()*this.list.length;
  13.         return this.list[parseInt(num)];
  14.     }
  15.     randint(start,end){
  16.         let new_length = end-start;
  17.         let numInt = Math.random(start,end)*new_length;
  18.         return parseInt(numInt);
  19.     }
  20. }
  21.  
  22. let greetings = ['Good Morning','Good Afternoon',"Good Night",'Good Bye',"Let  us meet"];
  23.  
  24. let random =  new Random(greetings);
  25. var message ;
  26. message = random.choice();
  27.  
  28. console.log(message);
  29.  
  30. console.log(random.randint(1,20))
  31.  
Add Comment
Please, Sign In to add comment