numbers

Make Random Numbers Tend / Average to a Specific Value

那年仲夏 提交于 2019-11-28 12:34:29
问题 How can I for example generate a list of random numbers between 0 and 1, but have them avarage at 0.8? I have written this little script in C++ that'll tell you what numbers got output. This question is not really C++ related though. #include <iostream> #include <random> #include <time.h> int main(int argCount, char** argVector) { std::cout << "Generating Randoms" << std::endl; float avarage = 0.F; srand(rand() + (int) time(NULL)); float ceiling = 0; float bottom = 1; for(unsigned int i = 0;

Function to check if string contains a number

你离开我真会死。 提交于 2019-11-28 12:33:28
I'm working on a project in c++ (which I just started learning) and can't understand why this function is not working. I'm attempting to write a "Person" class with a variable first_name, and use a function set_first_name to set the name. Set_first_name needs to call a function(the one below) to check if the name has any numbers in it. The function always returns false, and I'm wondering why? Also, is this the best way to check for numbers, or is there a better way? bool Person::contains_number(std::string c){ // checks if a string contains a number if (c.find('0') == std::string::npos || c

C++ Random number from 1 to a very large number (e.g. 25 million)

懵懂的女人 提交于 2019-11-28 12:20:27
How would you make a function that generates a random number from 1 to 25 million? I've thought about using rand() but am I right in thinking that the maximum number, RAND_MAX is = 32000 (there about)? Is there a way around this, a way that doesn't reduce the probability of picking very low numbers and doesn't increase the probability of picking high / medium numbers? Edit: @Jamey D 's method worked perfectly independent of Qt. You could (should) use the new C++11 std::uniform_real_distribution #include <random> std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<>

PHP - sorting an array of filenames with numbers?

旧巷老猫 提交于 2019-11-28 12:14:54
Can anyone tell me how to sort an array containing filenames that start with numbers? Because strings that start with 11, 12, 13 etc are considered lower than 2, it's scewing my results like this: [0] "1. File one.pdf" [1] "11. File eleven.pdf" [2] "12. File twelve.pdf" [3] "2. File two.pdf" [4] "3. File three.pdf" Is there anything I can do to sort these properly? You can use natsort . or natcasesort , which is case insensitive. If there is more than numbers (ie. diacritics), you should assure that you use proper locale . If it is not enough, ie. you want also sort number literals ("one",

Number().toLocaleString() has different format in different browsers

。_饼干妹妹 提交于 2019-11-28 12:13:09
I format a float to a locale string (Euro) and there are very different results in every browser. Is it possible to fix without an own function? var sum=2282.0000; var formated_sum = Number(sum.toFixed(2)).toLocaleString("de-DE", {style: "currency", currency: "EUR"}); Firefox result: 2.282,00 € Chrome result: 2.282 € IE result: 2.282,00 € Safari result: 2282 € Safari results are very much wrong, chrome results are not so much bad. Any Idea how to fix that without writing an own function for formatting? This question may already have an answer here: Inconsistent behavior of toLocaleString() in

Check if a String Ends with a Number in PHP

给你一囗甜甜゛ 提交于 2019-11-28 12:10:41
I'm trying to implement the function below. Would it be best to use some type of regex here? I need to capture the number too. function endsWithNumber($string) { $endsWithNumber = false; // Logic return $endsWithNumber; } $test="abc123"; //$test="abc123n"; $r = preg_match_all("/.*?(\d+)$/", $test, $matches); //echo $r; //print_r($matches); if($r>0) { echo $matches[count($matches)-1][0]; } the regex is explained as follows: .*? - this will take up all the characters in the string from the start up until a match for the subsequent part is also found. (\d+)$ - this is one or more digits up until

HTML5 input type number vs tel

筅森魡賤 提交于 2019-11-28 11:57:26
I'm working on updating some form inputs to HTML5. I'm not interested in validating the data so much as having the numeric key pad on mobile devices. Input type 'tel' seems to do what I want, I get the numeric keypad on iPad/mobile. Input type 'number' will also give me the numeric keypad, but it also includes the spinner box which I don't want. What I want to know is if it's safe to use type="tel" on a credit card input? Or should I use type="number" and try to disable the spinner somehow. I was reading that disabling the spinner can crash Chrome which isn't a trade off I'm willing to make.

R - Generate a sequence of numbers

折月煮酒 提交于 2019-11-28 11:56:59
I am trying to create sequences of number of 6 cases, but with 144 cases intervals. Like this one for example c(1:6, 144:149, 288:293) 1 2 3 4 5 6 144 145 146 147 148 149 288 289 290 291 292 293 How could I generate automatically such a sequence with seq or with another function ? I find the sequence function to be helpful in this case. If you had your data in a structure like this: (info <- data.frame(start=c(1, 144, 288), len=c(6, 6, 6))) # start len # 1 1 6 # 2 144 6 # 3 288 6 then you could do this in one line with: sequence(info$len) + rep(info$start-1, info$len) # [1] 1 2 3 4 5 6 144 145

JavaScript: Display positive numbers with the plus sign

泪湿孤枕 提交于 2019-11-28 11:52:34
How would I display positive number such as 3 as +3 and negative numbers such -5 as -5? So, as follows: 1, 2, 3 goes into +1, +2, +3 but if those are -1, -2, -3 then goes into -1, -2, -3 Guffa You can use a simple expression like this: (n<0?"":"+") + n The conditional expression results in a plus sign if the number is positive, and an empty string if the number is negative. You haven't specified how to handle zero, so I assumed that it would be displayed as +0 . If you want to display it as just 0 , use the <= operator instead: (n<=0?"":"+") + n // Forces signing on a number, returned as a

Finding all numbers in a string

試著忘記壹切 提交于 2019-11-28 11:35:45
Part of my app has an area where users enter text into a textBox control. They will be entering both text AND numbers into the textBox. When the user pushes a button, the textBox outputs its text into a string, finds all numbers in the string, multiplies them by 1.14, and spits out the typed text into a pretty little textBlock. Basically, what I want to do is find all the numbers in a string , multiply them by 1.14, and insert them back into the string. At first, I thought this may be an easy question: just Bing the title and see what comes up. But after two pages of now-purple links, I'm