I need to make a program that basically prints out a random value from an array, whether it be 'yes', 'no' or 'maybe' when the user hits the enter button. How do I do this, and how do I make it truly random?
dimanche 17 juin 2018
How can I random 3 different position in an 4x4 Array
I have a task which is creating an Dungeon Escape game, based on a 4x4 array. I first have to create 3 different postion for E(Exit),K(Key) and P(Player). How can I random those 3 in 3 different position in an array ?
Always getting the same numbers generated
i made a method to generate a number. But i'm always getting the same number in dice1 as in dice2. Why is this and how can i fix this?
int dice1 = GenerateNumber();
int dice2 = GenerateNumber();
Console.WriteLine(dice1);
Console.WriteLine(dice2);
static int GenerateNumber()
{
Random random = new Random();
int dice = random.Next(1, 7);
return dice;
}
How to get random numbers with different probabilities in different ranges in java?
I want to get an integer random number between 0 and 100 with different probabilities in different ranges. for example I want the probability of values between 0 and 20 to be 0.5 and the probability of values between 21 and 80 to be 0.4 and the probability of values between 81 and 100 to be 0.1 .
Is there any method or class in java or any library for java to do it? If no how can I do it myself?
samedi 16 juin 2018
Unable to store the same random string in cookies as in the variable
I am trying to store a randomly generated alphanumeric string into a cookie but the value set in the cookie is different from the variable. I would like to know how to store the same value in both the variable and the cookie.
Function to generate the random string
function str_random($len) {
$result = "";
$char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
$array = str_split($char);
for ($i=0; $i < $len; $i++) {
$randchar = $array[array_rand($array)];
$result .= $randchar;
}
return $result;
}
Storing random string in the cookie
$remember_token = str_random(250);
$dbh->update("users", array(":remember_token" => $remember_token, ), array(":id" => $user[0]["id"]));
setcookie("remember", $remember_token, time() + 60*60*24*7);
Now, if I try to make a var_dump of the cookie value and the value stored in the database (or the variable), it will return me two different string:
$result = $dbh->find_where("users", array(":id" => $user[0]["id"]));
var_dump($_COOKIE['remember']);
var_dump($remember_token);
var_dump($result[0]['remember_token']);
"Z0PuJEfjZFy87SDJiw2g6Yj1YrRL1xHQ9MDjr53h6OojzauagoRZinuGocxIrtPwQaZkSqyVOtfxBi0WF9VgFvTZIARCSZLyLACvmzCioFnTUtqt0r4vm33XKxcBpqFaWT1HAsKiQiuYVyDXwP9S7Y2VVxMyDxtQ3420MFM9OGmwprtDmYTnEpZ5U9vXl0QpMuLFdPw994OZcOjeg85bj9JUaYyGsTAjQZiEBULay3nPmyssKGQWJlEEtO" string(250)
//for the second line"J2D1X5GnWgE2T9UX4KtoRque8cqdRaiFifvj5E2r9qNlQoZOLHacatLNKJtIAg1oE51d2Jg8qr14h1a0qK8CjMymk97o23qVVbCcgh3GOPAqJlfU8TXiSZi5cvXmyD2FhXPKZDo2VqwM07GZNaP1JnTnhIcMEEFlLY4XU2JGrGDU66rnv1DuOBWWkwdQlBdzgd4Sbi6laUbNDqIg49s3xRc30su1aS6riZFGPHAlOYWVaLpNnMHBs515o1"
//for both the third line and the fourth line
If I had set the variable $random_token to a fixed string say foobar, the cookie would store the same value as the database would:
$remember_token = "foobar";
$$dbh->update("users", array(":remember_token" => $remember_token, ), array(":id" => $user[0]["id"]));
setcookie("remember", $remember_token, time() + 60*60*24*7);
var_dump($remember_token);
var_dump($_COOKIE["remember"]);
Output:
string(6) "foobar"
string(6) "foobar"
I suspect the problem being with the str_random function, generating a different token for the cookie than the one entered in the database. However, I do not know how to fix it.
For more information, here is the code for the method find_where and update:
public function find_where($tablename, $array) {
$req = "SELECT * FROM ". $tablename. " WHERE ". str_replace(":", "", key($array)). "=".key($array);
$smt = $this->conn->prepare($req);
$smt->execute($array);
$result = $smt->fetchAll();
return $result;
}
public function update($tablename, $update, $param) {
$keys_and_values =$this->format_keys_and_values($update);
$req = "UPDATE ". $tablename. " SET ". $keys_and_values. " WHERE ". str_replace(":", "", key($param)). "=". key($param);
$smt = $this->conn->prepare($req);
$execute = array_merge($update, $param);
$smt->execute($execute);
}
The code functions well generally (if I use those functions for another purpose, it would work)
How can i create multiple dictionaries for initial letters from a dictionary?
I want to create lists from a list. In the main list i have 1300+ dictionary words (most common) for English to Spanish. for example:
words = {"hablar":"to speak","reir":"to laugh","comer":"to eat"}
and 1300+ words like this. I do not want to seperate them manually for initials. I want to make a program to seperate them automatically like this;
a_words = {words with a}
b_words = {words with b}
c_words = {"comer":"to eat"}
.
.
.
.
.
h_words = {"hablar":"to speak"}
my program will automatically create dictionaries per initial letters. And i will make a random selection function so when i run the program, it will show me a word in Spanish and i will type it in English so i will be practicing. Thanks for all your help.
How to loop random number to make result equal user input summary
i have some problem like this. user(A) enter 500000 to my application and then i want to generate 50 random 5 digit number and when summary 50 random number need to equal 500000, How to do like i tried already but it's not working this is my code
int balane = 500000;
int nums = 50;
int max = balane / nums;
Random rand = new Random();
int newNum = 0;
int[] ar = new int[nums];
for (int i = 0; i < nums - 1; i++)
{
newNum = rand.Next(0, max);
ar[i] = newNum;
balane -= newNum;
max = balane / (nums - i - 1);
ar[nums - 1] = balane;
}
int check = 0;
foreach (int x in ar)
{
check += x;
}
the result that i tell you not working because in my array list i have negative & positive value but the result equal 500000.
How to solve this issue ? Thank you very much.
