I have an array called $ran = array(1,2,3,4);
I need to get a random value out of this array and store it in a variable, how can I do this?
I have an array called $ran = array(1,2,3,4);
I need to get a random value out of this array and store it in a variable, how can I do this?
if I understood your correctly I think this code is what you're looking for. In this case since you're using AJAX we'll be saving used questions like array in session.
<?php
if(!isset($_SESSION)) {
session_start();
$_SESSION['used'] = ((!isset($_SESSION['used'])) ? ([]) : ($_SESSION['used']));
}
//--------------------------------------------------------------------------
// 1) Connect to mysql database
//--------------------------------------------------------------------------
include 'DB.php';
$con = mysql_connect($host, $user, $pass);
$dbs = mysql_select_db($databaseName, $con);
$array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99];
$array_unique = [];
while(!empty($array_unique = array_diff($array, $_SESSION['used']))) {
//pick a random point from the array
$random = array_rand($array_unique, 1);
// Save the used element in session
$_SESSION['used'][] = $array_unique[$random];
//store the random question number
$question = $array_unique[$random];
// Select information from database and use array to assign to the selected row.
$query = mysql_query("SELECT `number`,`association`,`image_file`,`skeleton`,`sound`,`colour`,`comments` FROM ".$tableName." WHERE `number` = ".$question." LIMIT 0,1");
//fetch result to print on page
$row = mysql_fetch_row($query);
//Echo result as json
echo json_encode($row);
}
if(count($array_unique) == 0) {
$_SESSION['used'] = [];
}
?>
Just use while and array_rand
Shuffle it with shuffle()
:
shuffle($xbb);
Searching Google for php shuffle array will give you tons of results as well, by the way.
You can use tf.py_func
to wrap load_audio_file()
.
import tensorflow as tf
tf.enable_eager_execution()
def load_audio_file(file_path):
# you should decode bytes type to string type
print("file_path: ",bytes.decode(file_path),type(bytes.decode(file_path)))
return file_path
train_dataset = tf.data.Dataset.list_files('clean_4s_val/*.wav')
train_dataset = train_dataset.map(lambda x: tf.py_func(load_audio_file, [x], [tf.string]))
for one_element in train_dataset:
print(one_element)
file_path: clean_4s_val/1.wav <class 'str'>
(<tf.Tensor: id=32, shape=(), dtype=string, numpy=b'clean_4s_val/1.wav'>,)
file_path: clean_4s_val/3.wav <class 'str'>
(<tf.Tensor: id=34, shape=(), dtype=string, numpy=b'clean_4s_val/3.wav'>,)
file_path: clean_4s_val/2.wav <class 'str'>
(<tf.Tensor: id=36, shape=(), dtype=string, numpy=b'clean_4s_val/2.wav'>,)
UPDATE for TF 2
The above solution will not work with TF 2 (tested with 2.2.0), even when replacing tf.py_func
with tf.py_function
, giving
InvalidArgumentError: TypeError: descriptor 'decode' requires a 'bytes' object but received a 'tensorflow.python.framework.ops.EagerTensor'
To make it work in TF 2, make the following changes:
tf.enable_eager_execution()
(eager is enabled by default in TF 2, which you can verify with tf.executing_eagerly()
returning True
)tf.py_func
with tf.py_function
file_path
with file_path.numpy()
arc4random
can return negative numbers which would cause you problems since negative % positive = negative
A better approach would be to use arc4random_uniform
let randomIndex = arc4random_uniform(UInt32(cardArray.count))
EXC_BAD_INSTRUCTION seems like a bad exception to throw on a bounds error, but that does seem to be what you get.
You can also do just:
This is the way to do it when you have an associative array.