random

PHP generate a random minus or plus percentage of a given value

前提是你 提交于 2020-01-07 05:46:07
问题 I have a value, lets say its 1000. Now I have to generate a random minus or plus percentage of 1000. In particular I have to generate or a -20% of 1000 or a +20% of 1000 randomly. I tried using rand() and abs() but with no success.. Is there a way in PHP to achieve the above? 回答1: A bit of basic mathematics $number = 1000; $below = -20; $above = 20; $random = mt_rand( (integer) $number - ($number * (abs($below) / 100)), (integer) $number + ($number * ($above / 100)) ); 回答2: rand(0, 1) seems

Issue with checking new integer against previous one

試著忘記壹切 提交于 2020-01-07 03:44:10
问题 Im attempting to make something similar to the high low game. So far i can get a new integer between 1-25 to be generated. Ive tried to write a function that when you press the 'lower' button it checks the new integer against the previous one, and if it is less then text is displayed below saying 'You Were Correct' and updates the users score +1.. and if wrong 'You Were Wrong' displays instead and score is reset to 0. Whenever i press the lower button it gives me a new int but score is not

How to Shuffle & Echo 5 Random Elements from a String?

痞子三分冷 提交于 2020-01-07 03:21:08
问题 a question about getting three random words out of a big string of say 200 words: $trans = __("water paradise, chicken wing, banana beach, tree trunk")?> // $trans becomes "water paradijs, kippenvleugel, bananen strand, boom tak" // elements are separated by comma's and a space Now imagine I want to get 5 random elements from that $trans string and echo that . How can I do that? Code is welcome! Please keep this syntax in your answer: $trans = the original string $shufl = selective shuffle of

pass random numbers generated from Rand() to other pages for verification

心已入冬 提交于 2020-01-07 03:05:29
问题 I am trying to get a way to get the phone number of the users on my website verified. to do that I am integrating an SMS gateway. this will work like this:- step 1 /page 1 - User clicks on a button to get their phone number verified. Which runs the script that sends an SMS to their phone number with a random number and re-directs the user to page 2. Please see code below:- <?php // Start the session session_start(); ?> <html> <head> </head> <body> <form id="sms2" name="sms2" method="POST"

pass random numbers generated from Rand() to other pages for verification

坚强是说给别人听的谎言 提交于 2020-01-07 03:05:07
问题 I am trying to get a way to get the phone number of the users on my website verified. to do that I am integrating an SMS gateway. this will work like this:- step 1 /page 1 - User clicks on a button to get their phone number verified. Which runs the script that sends an SMS to their phone number with a random number and re-directs the user to page 2. Please see code below:- <?php // Start the session session_start(); ?> <html> <head> </head> <body> <form id="sms2" name="sms2" method="POST"

Simple A/B randomization script with PHP

江枫思渺然 提交于 2020-01-07 02:53:05
问题 I have two ad options in my theme, Adsense and "Custom". If the user enters code into the "Custom" field, it trumps whatever they have in the "Adsense" code field, so the theme places their custom ad atop the content area. If they have adsense enabled, and nothing in the custom ad spot, the theme places their adsense code there. I'd like to add an option to allow the user to opt to have my theme automatically rotate ads between adsense and their custom ads. The custom ads may be Ebay, Amazon,

Random order for group of rows in mysql

南楼画角 提交于 2020-01-07 02:50:14
问题 maybe it´s easy but i have no clue how to handle this correctly. I have the following table t1 with this data: ----------------- | id | gr_id | ----------------- | 1 | a | | 2 | a | | 3 | b | | 4 | b | | 5 | c | | 6 | c | | 7 | d | | 8 | d | ----------------- I would like to get randomly gr_ids like this: ----------------- | id | gr_id | ----------------- | 3 | b | | 4 | b | | 5 | c | | 6 | c | | 7 | d | | 8 | d | | 1 | a | | 2 | a | ----------------- Getting ordered gr_ids ascend and descend

Is there any way to generate uncorrelated random variables using Python?

笑着哭i 提交于 2020-01-07 02:46:05
问题 Suppose I want to generate two random variables X and Y which are uncorrelated and uniformly distributed in [0,1] . The very naive code to generate such is the following, which calls the random function twice: import random xT=0 yT=0 xyT=0 for i in range(20000): x = random.random() y = random.random() xT += x yT += y xyT += x*y xyT/20000-xT/20000*yT/20000 However, the random number is really a pseudo-random number which is generated by a formula , therefore they are correlated. How to

How can I create three random integers which sum to a specific value? (Python) [duplicate]

半世苍凉 提交于 2020-01-07 01:49:40
问题 This question already has answers here : Generate multiple random numbers to equal a value in python (6 answers) Closed last year . Let's say bob = 6 I want to create 3 random integers that have a sum of 106 (100 + whatever bob's original integer is. It could be 10, but in this case, it's 6). I've got: from random import * bob = 6 bob1 = (randint(0,100)) bob2 = (randint(0,100)) bob3 = (randint(0,100)) print bob1 print bob2 print bob3 I can generate integers, but how can I make sure the sum of

C Programming; Producing a 2-Dimensional Matrix with Random Numbers without Repetition

家住魔仙堡 提交于 2020-01-06 23:52:08
问题 I'd like to produce a 6x6 matrix by inserting random numbers to the elements in the row and column without repeating. This is my code so far. Thank you for your help! #include <stdio.h> #include <stdlib.h> int main(void) { int array[6][6]; int rows, columns; int random; srand((unsigned)time(NULL)); for(rows=0;rows<6;rows++) { for(columns=0;columns<6;columns++) { random=rand()%36+1; array[rows][columns] = random; printf("%i",array[rows][columns]); } printf("\n"); } return 0; } 回答1: To avoid