lundi 2 janvier 2017

Why does while loop run one time more?

As for the question, code inside loop gets called once more after its condition becomes false. I noticed it running a debug.

var bPrc=(Math.random()*120.0).toFixed(4);
                var aPrc=0;
                while (aPrc<bPrc){
                    aPrc=(Math.random()*120.0).toFixed(4);
                }




dimanche 1 janvier 2017

Swift 3 Music app and synced letters

I would like to place 12 characters, each in a different spot randomly spread across the storyboard.

Which is a good way to start out'? Using 12 text fields?

How could frames in that respect be useful. Maybe you have some basic information or any hints to get inspired with the possibilities Swift has to offer in that direction for commencing helpful studies

Thanks a lot




Java Game on Random Number [on hold]

I need assistance from anyone who can fix my code. Basically its a program to simulate two computer players playing a simple game of sticks.

This is how the program run:

Welcome to the game of sticks! There are initially 25 sticks on the board. Computer Player 1 chooses 5 sticks. Computer Player 2 chooses 7 sticks. Computer Player 1 chooses 7 sticks. Computer Player 2 wants to choose 7 sticks but is unable to. Computer Player 2, you lose.

But this is how my program run: Welcome to the game of sticks! There are initially 25 sticks on the board. Computer player 2 choose 6 sticks Computer player 2 choose 6 sticks Computer player 2 choose 6 sticks Computer player 2 choose 6 sticks Computer player 2 wants to choose 6 sticks but is unable to. Computer player 2 loses Computer player 2 wants to choose 6 sticks but is unable to. Computer player 2 loses Computer player 2 wants to choose 6 sticks but is unable to. Computer player 2 loses Computer player 2 wants to choose 6 sticks but is unable to. Computer player 2 loses Computer player 2 wants to choose 6 sticks but is unable to. Computer player 2 loses Computer player 2 wants to choose 6 sticks but is unable to. Computer player 2 loses.

This my code: Main

public class StickGameApp {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    StickBag s1 = new StickBag(25);

    System.out.println("Welcome to the game of sticks!");
    System.out.println("There are initially " + s1.getNumOfSticks() + " sticks on the board.");

    int minP = 1;
    int maxP = 2;
    int randP;

    int minN = 1;
    int maxN = 10;
    int randNum;

    randP = minP + (int)(Math.random()*(maxP));
    randNum = minN + (int)(Math.random()*(maxN));

    for (int i=0; i<10; i++)
    {
        if(s1.getNumOfSticks() > randNum)
        {
        System.out.println("Computer player " + randP + " choose " + randNum + " sticks ");
        s1.remove(randNum);
        }
        else
        {
            System.out.println("Computer player " + randP + " wants to choose " + randNum + " sticks but is unable to.");
            System.out.println("Computer player " + randP + " loses ");
        }
    }

}

}

And this is the class:

public class StickBag {

private int numOfSticks;

public StickBag(int numOfSticks)
{
    this.numOfSticks = numOfSticks;
}

public int getNumOfSticks() {
    return numOfSticks;
}

public void setNumOfSticks(int numOfSticks) {
    this.numOfSticks = numOfSticks;
}

public int remove(int n)
{
    numOfSticks = numOfSticks - n;
    return numOfSticks;
}

}




How to randomly position a button in swift3

I hit button a which changes the position of button b. How do i generate a function to make the position of button b random. Do I use arc4 random?




Pick random key based on weight and picked times

I am designing an advertising system that rotates randomly between ads depending on their weight (bid).

local ads = local ads = {
    ["a"] = {
        views = 0,
        bid = 10
    },
    ["b"] = {
        views = 0,
        bid = 1000
    },
    ["c"] = {
        views = 0,
        bid = 100
    },
    ["d"] = {
        views = 0,
        bid = 50
    },
    ["e"] = {
        views = 0,
        bid = 500
    },
    ["f"] = {
        views = 0,
        bid = 10
    },
}

I searched around and found the following algorithm:

  1. Get sum of all weighted numbers
  2. Pick a random number between 0 and sum
  3. Loop through table (ads) and if (random number) <= weight then return ad else random number = random number - weight

Using the algorithm and looping 1,000 times prints out

a 3
c 60
b 581
e 313
d 35
f 8

which is pretty okay. But as you can see ad f has received almost 3 times as much views ad a, even with equal weight (bid).

I tried to make the algorithm more fair by also taking into account the views the ad already got. I did this by reducing the weight with each view.

I couldn't make it work though and I wonder if someone can help me?




Determinant of random matrix nearly always zero

Hello and happy new year.

I implemented a matrix class, and one static method is to create a random matrix.

It's fairly simple and seems to work

public static Matrix random(int m, int n)
{
    Random r = new Random();
    Matrix rndMatrix = new Matrix(m, n);
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j++)
        {
            rndMatrix.setEntry(Math.random(), i, j);
        }
    }
    return rndMatrix;
}

The members of a matrix object are the various methods, 2 integers that represent number of rows and columns, and a double[][] that represents the data.

I also implemented a method that calculates the determinant of a given matrix.

It's rather complex but this is the code

public double determinant() throws RuntimeException // Via Gauss elimination. O(n^3)
{
    int m, n;
    double det = 1;
    m = this.getDimensions()[0];
    n = this.getDimensions()[1];

    if(m != n) // not a square matrix
    {
        System.out.println("determinant given non square matrix");
        RuntimeException e = new RuntimeException();
        throw e;
    }

    Matrix copy = new Matrix(this); // work on a copy, don't change original

    for(int i = 0; i < m-1; i++) // pivot row (and column) is i
    {
        double maxPivot = Math.abs(copy.getEntry(i, i));
        int maxPivotRow = i;
        for(int j = 1; j < m; j++) // perform partial pivoting for numerical stability
        {
            double potentialPivot = copy.getEntry(j, i);
            if(Math.abs(potentialPivot) > maxPivot)
            {
                maxPivot = potentialPivot;
                maxPivotRow = j;
            }
        } //now our pivot is at copy[maxPivotRow][i]

        if(maxPivotRow != i) //need to swap rows
        {
            det *= -1; //each row swap changes sign of determinant

            for(int j = 0; j < m; j++) //change row i and row maxPivotRow. j runs on columns
            {
                double temp = copy.getEntry(i, j);
                copy.setEntry(copy.getEntry(maxPivotRow, j), i, j);
                copy.setEntry(temp, maxPivotRow, j);
            } // now our pivot is at copy[i][i]
        }

        if(copy.getEntry(i, i) == 0) //Pivot is 0, meaning determinant is 0
            return det;

        for(int j = i + 1; j < m; j++) // rank!
        {
            double coefficient = copy.getEntry(j, i) / copy.getEntry(i, i);
            for(int k = i; k < m; k++)
            {
                double currentElement = copy.getEntry(j, k);
                copy.setEntry(currentElement - coefficient * copy.getEntry(i, k), j, k);
            }
        } 
    }// copy is now row reduced, upper triangular. determinant is product of diagonal

    for(int i = 0; i < m; i++)
    {
        det *= copy.getEntry(i, i);
    }

    return det;
}

the calculation is done via gaussian elimination (with partial pivoting) and then multiplying the elements on the diagonal. I have tested it several times and it seems to work.

the problem is that it doesn't work on random matrices, or well, I'm getting an unexpected behavior.

9 out of 10 times i'll create a random matrix and calculate its determinant, it will print 0.0 or -0.0.

This seems odd. "Most" matrices are invertible. So why does this happen? Is there a bug? Do I not understand the math?




How to generate random php slug/url and insert in database?

i want to generate 6 character random url for each post like ac7Lmn,uYoXo etc when user clicks submit button .

if the slug already exists in database then generate another.

How can i do this please help.