mercredi 24 décembre 2014

Working with random integers

Here is the assignment: For this program, there are two "parts". The first part will run the trials and determine how many caps each trial opens before finding a winning cap. Every trial (person) will be a winner. The number of caps opened by each trial is written to the file. (The output file should contain only integers). (This is where you will use nested loops...a for and a while...not two for's)


The second part of the program will read in the values and calculate the average. The average should be between 4.5 and 5.5 since there is a 1 in 5 chance of winning.


I know I have many mistakes, I want someone to tell me where to start. I do not know much about java.


My code:



int randNum = 0;
Random randNumList = new Random();

int counter = 0;
Scanner in = new Scanner(System.in);
System.out.println("How many trials will there be?");
int trials = in.nextInt();

int winner = 0;

PrintWriter outFile = new PrintWriter (new File("cap.txt"));
//run trials
for (int loop = 1; loop <= trials; loop++)
{
//select random number until 5 is selected
randNum = randNumList.nextInt(6);
for (randNum = randNumList.nextInt(6); randNum == 5; randNum++)
{
randNum = randNumList.nextInt(6);
counter++;
}

outFile.println(loop + " " + randNum);
}


outFile.close ( ); //close the file when finished

String token = " ";
Scanner inFile = new Scanner(new File("cap.txt"));

while (inFile.hasNext())
{
token = inFile.next();
if(token.equals("5"))
winner++;
}

double average = winner/counter;
System.out.println("The average number is " + average);




Random CSS value

I have this page where I have 3 triangles made purely out of CSS. Every time someone loads the page I want the triangle to have different sizes.


Here's the thing illustrated in a JSFiddle : http://ift.tt/1AK9ZMz


What I would like to do is to randomly set the value of border-width of every triangle at page load.


I was thinking doing that with JavaScript with a Math.random or something but I didn't manage to do it, nor found some help on the internet (I don't know how to write in Javascript).


I would appreciate a little help or guidance for this. Thanks a lot





Repetitive usage of Java's SecureRandom

I'm a little bit confused about the usage of SecureRandom. I need to generate n random numbers in a loop. Is it secure to use the same SecureRandom instance for each generation? Are there any difference between the solutions below in terms of cryptographic strength?


1) Single instance without seeding



SecureRandom sr = new SecureRandom();
for(int i = 0; i < n; ++i) sr.nextInt();


2) New instance for each generation



for(int i = 0; i < n; ++i) new SecureRandom().nextInt();


3) Single instance with seeding



SecureRandom sr = new SecureRandom()
for(int i = 0; i < n; ++i) {
byte[] seed = sr.generateSeed(32);
sr.setSeed(seed);
sr.nextInt();
}




Cross-platform cross-language cross-everything actually deterministic random number generator

I'm looking for an algorithm to generate random numbers from a given seed but with the particular requirement that it will always generate the same sequence of number regardless of the underlying computer architecture or language implementation.


I already know of Mersenne Twister, however, the numbers it generates differ when using different implementations (i.e. C MT vs Javascript MT).


Do algorithms with this property exist? Also, I don't need a state-of-the-art RNG, I don't even need it to be cryptographically secure, I just want to drive a "random" simulation on one place and have it follow the same behavior on a different implementation.





random generator and openCV in c++

This is my function for generating random white noise in openCV and c++. Can anyone point me to the real problem ?



int i,j;

int h = image.cols;
int w = image.rows;

for(int k=0; k<n ; k++)
{

i = Random(h);
j = Random(w);


if(image.channels() == 1)
{
image.at<uchar>(j,i) = 255;
}
else if (image.channels() == 3)
{
image.at<cv::Vec3b>(j,i)[0] = 255;
image.at<cv::Vec3b>(j,i)[1] = 255;
image.at<cv::Vec3b>(j,i)[2] = 255;

}

}


![enter image description here][1]





mardi 23 décembre 2014

How to use a random generated number in a parameter

So i am trying to change the enemies health based on a randomly number. When I try to pass a number using the random class I get an error:



The method changeEnemyHealth(int, int) in the type enemyStats is not applicable for the arguments (int)


And the code:



int enemyDamage = Random_damage.nextInt(31) + 270;
enemyStats.changeEnemyHealth(enemyDamage);
playerStats.changePlayerMana(-4);

public class enemyStats{

public static void changeEnemyHealth(int EHealth){
EnemyHealth += EHealth;
}
}




Random Choice is out of list range in Python?

I am trying to create a program that will guess the number you entered. I was trying to get the computer to get the number in as few guesses as possible so I am doing something like a binary search. I keep getting a Index out of range when I run the code and it gets to total = total[the_low_one:computer2_guess]. I am confused why it is out of range. Should I be adding or subtracting one to the_low_one each time it hits a new low so it stays in range? I would appreciate any help as I am lost. Thanks a bunch in advance! Code:



def second_computer():
global computer2_score
computer2_score=0
computer2_guess= -1
the_low_one=1
the_high_one=1000000
total= range(1,1000000)
while computer2_guess != pick_number:
computer2_guess=random.choice(total)
if computer2_guess>pick_number:
total=total[the_low_one:computer2_guess]
the_high_one=computer2_guess-1
else:
total=total[computer2_guess:the_high_one]
the_low_one=computer2_guess+1
computer2_score+=1