lundi 22 février 2021

How to make a random number generator and customise how many duplicates in R

I just want to know how to make a random number generator ranging from 1,000,000 - 999,999,999, customise how many duplicates I can have, and counts how many combinations were made.

Thanks.




dimanche 21 février 2021

Implementing Conditional Random Field for language processing in Python

I want to implement a CRF for parsing from scratch. But I don't have much experience with Python, so unfortunately there isn't much code that I can post... How can a very simple model be implemented?




bivariate distribution, random variable

Suppose X and Y are continuous random variables such that Y = x^3

Determine the Cumulative Distribution Function (CDF) of Y in terms of the CDF of X.

Express the bivariate CDF of X and Y in terms of the CDF of X.

Determine the copula of the bivariate distribution of part (2nd).

Determine the copula of the bivariate distribution of X and Z, where Z = -x^3.




Why wont my code write to a file? in java

I am trying to get everything in my do-loop to write to the file but it will not work. I am using multiple methods in one class. It is not shown but I did import the random number generator class and the IO exception class. I want to write the data from these different methods into the file. Where did I go wrong?

Here is my code:

public class ArtificialData{ 
    
       public static String getRandomMake(){
          String[] makes = {"Toyota", "BMW", "Dodge", "Carriage", "Ferrari", 
                            "Sled", "Chevrolet", "Hyundai", "Ford", "Mercedes", "Honda"};
          Random rand = new Random();
          int randomIndex = rand.nextInt(makes.length);
          String randomMake = makes[randomIndex];
          System.out.print(randomMake +", ");
          
          return randomMake;       
       }
       
       public static String getRandomModel(){
          String[] models = {"335xi", "Civic", "Camry", "G500", "Huracan", "3500", "Genesis", 
                              "Corvette", "Viper", "Model S", "Corolla"};
          Random rand = new Random();
          int randomIndex = rand.nextInt(models.length);
          String randModel = models[randomIndex];
          System.out.print(randModel +", " );
          
          return randModel;   
       }
       
       public static String getRandomMPG(){
          int lowest = 7, highest = 80;
          Random rand = new Random();
          
          double randomDouble = rand.nextDouble();
          int randomInt = rand.nextInt((highest - lowest)+1)+lowest;
          String randMPG = String.format("%.1f", (randomInt + randomDouble));
          System.out.print(randMPG + ", ");
          
          return randMPG;
       }
       
       public static String getRandomColor(){
          String[] colors = {"Green", "White", "Red", "Purple", "Blue", "Pink", 
                               "Black", "Lime", "Brown", "Maroon", "Orange", "Yellow"};
          Random rand = new Random();
          int randomIndex = rand.nextInt(colors.length);
          String randColor = colors[randomIndex];
          System.out.print(randColor + ", ");
          
          return randColor;
       }
       public static int getRandomNumDoors(){
          Random rand = new Random();
          int randDoors = rand.nextInt(4)+1;
          System.out.print(randDoors + ", ");
                
          return randDoors;
       }
       public static void main(String[] args)throws IOException{
                System.out.println("Finished..");
          
          FileWriter createData = new FileWriter("c:/temp/CarData.txt", true);
          int x = 0;
          
          do{
            ++x;
            
             createData.write(getRandomMake);
             createData.write(getRandomModel);
             createData.write(getRandomMPG);
             createData.write(getRandomColor);
             createData.write(getRandomYear);
             createData.write(getRandomDoors);
             System.out.print("\n");       
           }
           while (x<=3000);
          
              
          //closing file
          createData.close();
    
       }
       
       }



How to generate my code to output a specific amount of times

I want to run and print out the loop 3000 times but every time I try to run it, it only prints whatever I called in the loop, once. I am using the random number generator to execute this or at least I think I am doing so. It is not shown in my code but I did import the random class and the IO exception class. What did I do wrong?

Here is my code:

public class ArtificialData{ 
    
   public static String getRandomMake(){
      String[] makes = {"Toyota", "BMW", "Dodge", "Carriage", "Ferrari", 
                        "Sled", "Chevrolet", "Hyundai", "Ford", "Mercedes", "Honda"};
      Random rand = new Random();
      int randomIndex = rand.nextInt(makes.length);
      String randomMake = makes[randomIndex];
      System.out.print(randomMake +", ");
      
      return randomMake;       
   }
 
   public static String getRandomModel(){
      String[] models = {"335xi", "Civic", "Camry", "G500", "Huracan", "3500", "Genesis", 
                          "Corvette", "Viper", "Model S", "Corolla"};
      Random rand = new Random();
      int randomIndex = rand.nextInt(models.length);
      String randModel = models[randomIndex];
      System.out.print(randModel +", " );
      
      return randModel;   
   }
   
   public static String getRandomMPG(){
      int lowest = 7, highest = 80;
      Random rand = new Random();
      
      double randomDouble = rand.nextDouble();
      int randomInt = rand.nextInt((highest - lowest)+1)+lowest;
      String randMPG = String.format("%.1f", (randomInt + randomDouble));
      System.out.print(randMPG + ", ");
      
      return randMPG;
   }
   
   public static String getRandomColor(){
      String[] colors = {"Green", "White", "Red", "Purple", "Blue", "Pink", 
                           "Black", "Lime", "Brown", "Maroon", "Orange", "Yellow"};
      Random rand = new Random();
      int randomIndex = rand.nextInt(colors.length);
      String randColor = colors[randomIndex];
      System.out.print(randColor + ", ");
      
      return randColor;
   }
   
   public static int getRandomYear(){
      Random rand = new Random();
      int randYear = ((2020)+1950);
      System.out.print(randYear + ", ");
      
      return randYear;   
   }

   public static int getRandomNumDoors(){
      int randDoors = ((4)+1);
      System.out.print(randDoors + ", ");
            
      return randDoors;
   }

   public static void main(String[] args)throws IOException{
            System.out.println("Finished..");
      
      FileWriter createData = new FileWriter("CarData.txt", true);
      int x = 0;
      do{
        ++x;
         String randMake = getRandomMake();
         String randModel = getRandomModel();
         String randMPG = getRandomMPG();
         String randColor = getRandomColor();
         int randYear = getRandomYear();
         int randDoors = getRandomNumDoors();
       
       }
       while (x>=3000);
          
      //clossing file
      createData.close();

   }
   
   }



setting constraints on random number list generation in python

I have the following random number list generator, each sample is from the bounds m=0 to the respective elements 'g' of list gen_limit

import numpy as np
m=0
gen_limit=[100,20,50,200,10,50]
rand_gen=list([np.random.uniform(m,g) for g in gen_limit])
>>> [54.99, 2.09, 10.61, 143.44, 7.08, 35.34]

is there any way to introduce a constraint, for example where rand_gen equals x = 100.0 ? e.g. sample output would be

[18.2575,2.09,10.61,26.6225,7.08,35.34,253.55] == 100



Changing element position while window resize with overlap checking

I'm completely stuck.

I've got a container where I randomly creating a five span elements (dice) with overlap checking.

Here you can find container CSS:

&__dice {
    position: relative;
    height: 200px;
    background-color: rgb(3, 70, 3);

    @include mq(mobileLarge) {
        height: calc(100% - 37.5px);
    }
}

I'm using position: absolute for each span element to place them inside the board (container).

And here JS code:

  1. createDice
    createDice(die, positions, dieIndex) {
        const span = document.createElement("span");
        positions = this.addRandomPosition(positions);
        span.classList.add("dice-area__die", "fas", this.diceClasses[die]);
        span.style.left = positions[dieIndex].posX + "px";
        span.style.top = positions[dieIndex].posY + "px";
        span.setAttribute("data-die", dieIndex);
        span.setAttribute("data-choose", "noChoose");
        return span;
    }

die is die number which is changing to span element and position is empty array.

  1. addRandomPosition
    addRandomPosition(positions) {
        let posX = 0;
        let posY = 0;
        
        const maxBoardWidth = this.getBoardWidth();
        const maxBoardHeight = this.getBoardHeight();

        do {
            posX = Math.floor(Math.random() * (maxBoardWidth - this.dieSize));
            posY = Math.floor(Math.random() * (maxBoardHeight - this.dieSize));
        } while (this.checkOverlapping(posX, posY, positions));

        positions.push({ posX, posY });
        return positions;
    }
  1. chceckOverlapping

    checkOverlapping(pX, pY, positions) {
        for (let i = 0; i < positions.length; i++) {
            const newPosX = pX;
            const oldPosX = positions[i].posX;
            const rightNewPosX = pX + (this.dieSize + 5);
            const rightOldPosX = positions[i].posX + (this.dieSize + 5);
            const newPosY = pY;
            const oldPosY = positions[i].posY;
            const bottomNewPosY = pY + (this.dieSize + 5);
            const bottomOldPosY = positions[i].posY + (this.dieSize + 5);

            if (rightNewPosX < oldPosX || newPosX > rightOldPosX || newPosY > bottomOldPosY || bottomNewPosY < oldPosY) {
                continue;
            }
            return true;
        }
        return false;
    }

The problem is, that positions I've created are "permanent". When I resize the window, container width (and height in two breakpoints) are changing but my positions are still the same.

I've got two breakpoints when I'm changing whole container - (min-width: 1024px) and (min-width: 800px). In that breakpoints my container changing width and height (height is constant -> 400px and 200px for each breakpoint).

I tried to use matchMedia, addEventListener("resize") and addEventListener("change") for matchMedia inside createDice and addRandomPosition function, but I've always got an infinite loop.

So, my question is: How to change positions of those five span elements, when I resize the window but with overlap checking?

I don't want to change X and Y position every time I resize the window. That means:

  1. Change X position (if it's necessary, for example, when one die is near the board border), but here dice must stop changing X position when they found left board border + elements can't overlap themselves.
  2. Change X and Y position when window break the breakpoints.