jeudi 24 décembre 2015

How to display the elements stored in an integer arraylist?

I've tried running this code for several times (re-opening eclipse IDE) but this doesn't change anything, the code simply won't run.. This a part of a PRNG.I've even tried to run the comment alone but all i get is[]System.out.println(objprng2.clcg(2,10,50,44,54,67,6,6,4,6,2,5,5,4,5,5,4,5));

package Firstage;

import java.util.*;

public class prng2 {

static List<Integer> seq = new ArrayList<>();

public List<Integer> clcg(int mul_1,int seed_1,int inc_1,int mod_1,int mul_2,int seed_2,int inc_2,int mod_2,int mul_3,int seed_3,int inc_3,int mod_3,int mul_4,int seed_4,int inc_4,int mod_4,int newno,int more)
{
    int a1=(4*mul_1)+1;
    int c1=(2*inc_1)-1;
    int m1=(int) Math.pow(2,(mod_1 +more));

    int a2=(4*mul_2)+1;
    int c2=(2*inc_2)-1;
    int m2=(int)Math.pow(2,(mod_2 +more));

    int a3=(4*mul_3)+1;
    int c3=(2*inc_3)-1;
    int m3=(int)Math.pow(2,(mod_3 +more));

    int a4=(4*mul_4)+1;
    int c4=(2*inc_4)-1;
    int m4=(int)Math.pow(2,(mod_4 +more));

    int X=0;

    int period=(((m1)-1)*((m2)-1)*((m3)-1)*((m4)-1))/8;
    int pc=0;
    int newlim=0;

    while (pc < period)
    {
        int i=1;
        int p=0;
        seed_1= ((a1 * seed_1)+c1)%m1;
        seed_2= ((a2 * seed_2)+c2)%m2;
        seed_3= ((a3 * seed_3)+c3)%m3;
        seed_4= ((a4 * seed_4)+c4)%m4;
        X=((seed_1)+(seed_2)+(seed_3)+(seed_4))% ((m1)-1);
        int R = X % 256;
       // System.out.println("for the "+i+" time R: "+R+" seq:"+seq.get(p) );
        seq.add(R);

        newlim= newlim + 1;
        if (newlim == newno)
           {
            break;
           }               
        else
            {
            pc=pc+1;
            }
   // i++;
   // p++;
    }
    return seq;
    }

public static void main (String[] args)
{
    prng2 objprng2 = new prng2();
    objprng2.clcg(2,10,50,44,54,67,6,6,4,6,2,5,5,4,5,5,4,5);
    //System.out.println(objprng2.clcg(2,10,50,44,54,67,6,6,4,6,2,5,5,4,5,5,4,5));

        for(Integer element : seq) 
{   
        System.out.print((element));
} 
}
}




How do I make javascript display random names without repeats?

I need to be able to click on a button, then have a few different names come out in a random order without any name being repeated. Displaying random names is simple enough, but I cannot avoid repeats. Here's what I have and I don't understand why it doesn't work.

<script>

var players = []
players[0] = "Billy";
players[1] = "Alex";
players[2] = "Kevin";
players[3] = "Fred";

function tournament(amount) {
    var a = 0, i = 0, place = 1;
    randomlyselect:
    while (a<amount) {
        var randomplayer = Math.floor(Math.random()*(players.length));
        if (players[randomplayer] === document.getElementById('player1').innerHTML || document.getElementById('player2').innerHTML || document.getElementById('player3').innerHTML) {
            continue randomlyselect;
            } 
        else {
            document.getElementById('player'+place).innerHTML = players[randomplayer];
            place++;
            a++;
        }
    }

}

</script>

<button onClick="tournament(4)">Tournament Results</button>

<p id="player1"></p>
<p id="player2"></p>
<p id="player3"></p>
<p id="player4"></p>

I know the problem is the if statement, but I don't know how else I would check if a name was already being displayed. If you know what's wrong with my method of checking if something was already picked or a better way of avoiding repeats please let me know.




mercredi 23 décembre 2015

Randrange dependant on variable - Python

Im trying to make random co-ords for a object spawning onto a canvas(The canvas is the size of the users screen so the range is ever changing.)

random.randint(12.5, int(resX-12.5))

This is the line of code resX being the variable that stores the size of the users screen resolution, 12.5 being the radius of the shape being spawned.

The error i'm getting is that the resX part isn't an integer ValueError: non-integer arg 1 for randrange()

Im not sure if theres a way to use a variable within the random line?




Questions about Random and Int (Java)

Assuming I generate a random number between 1-10, how do I use the random number generated into a for loop? Is there a way to convert random to int?

public class CityTravelerApp {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    // TODO code application logic here

    Queue myQueue= new Queue(80);

    //a. Generate a random number called num between 1 and 10, inclusive. 
    Random number = new Random();
    System.out.println(number.nextInt(10)+1);


    for(int i =1;i<=number;i++){
        System.out.println("Enter the name of country #" + i);
        String name = input.next();
    }


    }




PHP/CSS Random Background

Trying to get a random background between two images.

The PHP (Before Doctype in index.php):

<?php
$bg = array('back1.jpg', 'back2.jpg' ); // array of filenames
$i = rand(0, count($bg)-1); // generate random number size of the array
$selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen
?>

CSS:

body {
  height: 100%;
  margin: 0;
  color: #474747;
  font: 13px/23px  'Exo 2', sans-serif;
  min-width: 1186px;
  background: url( ../img/<?php echo $selectedBg; ?>) no-repeat 50% 50%;
  background-size: cover;
  background-attachment: fixed;
}

The issue is that the background isn't set to either of the images!




arc4random_uniform negative 100 to positive 100

Trying to figure out how to make this little ball spawn anywhere from the center (self.frame.width / 2) to (self.frame.width / 2 - 100) and (self.frame.width / 2 + 100)

I found this code on stackoverflow and it seems fine, but my game crashes when the ball is about to spawn

ball.position = CGPoint(x: self.frame.width / 2 + (CGFloat(arc4random_uniform(201) - 100)), y: self.frame.height)

Help would be greatly appreciated as always <3




Draw random numbers from an incomplete uniform distribution

Goal

I am trying to build a function that allows me to draw a specific number of random numbers from an an incomplete uniform distribution.

What do I call an incomplete uniform distribution?

I call an incomplete uniform distribution a probability distribution where each value of X within a series of boundaries have an equal probability to be picked. In other words it is a uniform distribution with holes as represented below

x = list(12:25, 34:54, 67:90, 93:115)
y = 1/sum(25-12, 54-34, 90-67, 115-93)
plot(y=rep(y, length(unlist(x))), x=unlist(x), type="n", ylab="Probability", xlab="X")
for (xi in x)
{
    points(xi,rep(y, length(xi)), type="l", lwd=4)
}

enter image description here

Ugly Solution

Here is a slow and ugly solution

IncompleteUnif = function(n,b)
{
    #################
    # "n" is the desired number of random numbers
    # "b" is a list describing the boundaries within which a random number can possibly be drawn.
    #################
    r = c() # Series of random numbers to return
    for (ni in n)
    {
        while (length(r) < n) # loop will continue until we have the "n" random numbers we need
        {
            ub = unlist(b)
            x = runif(1,min(ub), max(ub)) # one random number taken over the whole range
            for (bi in b) # The following loop test if the random number is withinn the ranges specified by "b"
            {
                if (min(bi) < x & max(bi) > x) # if found in one range then just add "x" to "r" and break
                {
                    r = append(r,x)
                    break
                }
            }
        }
    }
    return (r)
}

b = list(c(5,94),c(100,198),c(220,292), c(300,350))
set.seed(12)
IncompleteUnif(10,b)
[1]  28.929516 287.132444 330.204498  63.425103  16.693990  66.680826 226.374551  12.892821   7.872065 140.480533