我有一个变量,我给出-2.0f
和2.0f
之间的随机范围(Random.Range(-2.0f, 2.0f
);在Unity / C#中,但我不希望它在-0.6f
和0.6f
之间选择一个数字。
我如何做到这一点?
答案 0 :(得分:3)
循环的东西对我来说似乎有点浪费。毕竟,你最终可能会产生一堆无法使用的数字。为什么不从两个允许范围生成两个数字,然后随机选择一个返回?
function my_send_email($name,$email, $current_user, $status, $subject, $Message) {
//most of the code here is from https://github.com/PHPMailer/PHPMailer/tree/master/examples
require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once ABSPATH . WPINC . '/class-smtp.php';
// build message body`enter code here`
$body = '
<html>
<body>
<h1>My Email</h1>
</body>
</html>
';
try {
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "mail.example.com";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "contact@example.com";
//Password to use for SMTP authentication
$mail->Password = "1234";
//Set who the message is to be sent from
$mail->setFrom('contact@example.com', 'sender_name');
//Set an alternative reply-to address
$mail->addReplyTo('alternative_contact@example.com', 'alternative_contact_name');
//Set who the message is to be sent to
$mail->addAddress($email, $name);
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents(dirname(__FILE__) .'/contents.html'));
$mail->MsgHTML($body);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
$success=$mail->send();
//send the message, check for errors
if ($success) {
return array('mailError' => false, 'message' => 'email sent! ' . $mail->ErrorInfo , 'body'=>$body);
} else {
return array('mailError' => true, 'message' => 'email error! ' . $mail->ErrorInfo , 'body'=>$body);
}
} catch (phpmailerException $e) {
return array('mailError' => false, 'message' => 'email error! ' . $e->errorMessage() , 'body'=>$body); //Pretty error messages from PHPMailer
} catch (Exception $e) {
return array('mailError' => false, 'message' => 'email error! ' . $e->getMessage() , 'body'=>$body); //Boring error messages from anything else!
}
}
function my_register_endpoints(){
register_rest_route(
'my_namespace/v1',
'/abc/',
array(
'methods' => 'POST',
'callback' => 'my_end_point',
'args' => array(
.....
),
'permission_callback' => function (WP_REST_Request $request) {
if(!is_user_logged_in()){
.....
}
return true;
}
)
);
}
add_action('rest_api_init','my_register_endpoints');
//endpoint
function my_end_point(WP_REST_Request $request){
global $current_user;
$sender = wp_get_current_user();
$shortMsg=$request['shortMessage'];
$subject="Some text";
$email_resualt=my_send_email("reciver_name","reciver_email",$sender,$status,$subject,$shortMsg);
if($email_resualt['mailError']==false){
process.. $itemes, $item ..
$message='email sent!';
return array('message' => $message,'items' => $items, 'item'=>$item);
} else {
return new WP_Error('email error',__('some message','email-error'),$request['id']);
}
}
你也可以做一个随机数,然后随机决定是否应该将其作为负数或正数返回。下面的代码也避免了OrderBy方法,我被告知它不适用于Unity和iOS
var candidates = new[] {Random.Range(-2.0f, -0.6f), Random.Range(0.6f, 2.0f)};
return candidates.OrderBy(c => Random.Next()).First();
另外,严格来说,虽然严格地说,在原始问题的范围之外,如果你不能假设两个范围是相同的(绝对值),并且你也不能使用OrderBy,你可以这样写:
var rand = Random.Range(0.6f, 2.0f);
if (Random.Range(0, 100) <= 50)
{
rand = rand * -1f;
}
return rand;
答案 1 :(得分:2)
怎么样:
var delta = Random.Range(-1.4f, 1.4f);
var result = delta >= 0 ? delta + 0.6f : delta - 0.6f;
或者也许:
var delta = Random.Range(-1.4f, 1.4f);
var abs = Mathf.Abs(delta);
var sign = Mathf.Sign(delta);
var result = sign*(0.6f + abs);
编辑:更改了内容以避免使用三元运算符并删除了错误的注释。谢谢@sh1!
答案 2 :(得分:1)
凯西的回答看起来不错。我只是不知道如何使其与其他价值观一起使用。下面的答案提供了一个可重用的代码,可以轻松使用其他值。
它会生成第一个数字。该数字将用于确定是否应生成低于要排除的最小数字的数字或高于要排除的最大数量的数字。它生成的第二个数字是实际的随机数。
float Range(float min, float max, float ignoreFrom, float ignoreToEnd)
{
int dirRand = UnityEngine.Random.Range(1, 3); //1 to 2 random number
float finalRandomNum = 0;
//If 1, generate number below ignoreFrom, min
if (dirRand == 1)
{
ignoreFrom += 0.00001f; //To make make ignoreFrom exclusive (Remove if you want to include it)
finalRandomNum = UnityEngine.Random.Range(ignoreFrom, min);
}
//If 2, generate number above ignoreToEnd
else if (dirRand == 2)
{
ignoreToEnd += 0.00001f; //To make make ignoreToEnd exclusive (Remove if you want to include it)
finalRandomNum = UnityEngine.Random.Range(ignoreToEnd, max);
}
return finalRandomNum;
}
要对其进行测试,我们可以在-2.0f
和2.0f
之间生成80个随机数,其中排除分为-0.6f
和最大0.6f
。
for (int i = 0; i < 80; i++)
{
Debug.Log(Range(-2f, 2f, -0.6f, 0.6f));
}