range

Javascript: Generate a random number within a range using crypto.getRandomValues

孤街浪徒 提交于 2019-11-26 20:57:20
问题 I understand you can generate a random number in JavaScript within a range using this function: function getRandomInt (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } Courtesy of Ionuț G. Stan here. What I want to know is if you can generate a better random number in a range using crypto.getRandomValues() instead of Math.random(). I would like to be able to generate a number between 0 and 10 inclusive, or 0 - 1, or even 10 - 5000 inclusive. You'll note Math.random()

group by range in mysql

本小妞迷上赌 提交于 2019-11-26 20:42:22
问题 Table: new_table user_number | diff 2 | 0 1 | 28 2 | 32 1 | 40 1 | 53 1 | 59 1 | 101 1 | 105 2 | 108 2 | 129 2 | 130 1 | 144 |(result) v range | number of users 0-20 | 2 21-41 | 3 42-62 | 1 63-83 | 2 84-104 | 1 105-135| 0 136-156| 3 select t.range as [range], count(*) as [number of users] from ( select case when diff between 0 and 20 then ' 0-20' when diff between 21 and 41 then ' 21-41' when diff between 42 and 62 then ' 42-62' when diff between 63 and 83 then ' 63-83' when diff between 84

Initialize std::array with a range (pair of iterators)

烂漫一生 提交于 2019-11-26 20:24:14
问题 How can I initialize an std::array from a range (as defined by a pair of iterators)? Something like this: vector<T> v; ... // I know v has exactly N elements (e.g. I just called v.resize(N)) // Now I want a initialized with those elements array<T, N> a(???); // what to put here? I thought array would have a constructor taking a pair of iterators, so that I could do array<T, N> a(v.begin(), v.end()) , but it appears to have no constructors at all! I know I can copy the vector into the array,

Is there a reason that we cannot iterate on “reverse Range” in ruby?

百般思念 提交于 2019-11-26 20:18:18
I tried to iterate backwards with using a Range and each : (4..0).each do |i| puts i end ==> 4..0 Iteration through 0..4 writes the numbers. On the other Range r = 4..0 seems to be ok, r.first == 4 , r.last == 0 . It seems to be strange to me that the construct above does not produce the expected result. What is the a reason for that? What are the situations when this behaviour is reasonable? A range is just that: something defined by its start and end, not by its contents. "Iterating" over a range doesn't really make sense in a general case. Consider, for example, how you would "iterate" over

How can I generate a random number in a certain range?

China☆狼群 提交于 2019-11-26 19:56:15
问题 How can I create an app that generates a random number in Android using Eclipse and then show the result in a TextView field? The random number has to be in a range selected by the user. So, the user will input the max and min of the range, and then I will output the answer. 回答1: To extend what Rahul Gupta said: You can use the Java function int random = Random.nextInt(n) . This returns a random int in the range [0, n-1] . I.e., to get the range [20, 80] use: final int random = new Random()

How to convert sequence of numbers in an array to range of numbers

回眸只為那壹抹淺笑 提交于 2019-11-26 19:44:06
问题 In javascript how to convert sequence of numbers in an array to range of numbers? eg. [2,3,4,5,10,18,19,20] to [2-5,10,18-20] 回答1: Here is an algorithm that I made some time ago, originally written for C#, now I ported it to JavaScript: function getRanges(array) { var ranges = [], rstart, rend; for (var i = 0; i < array.length; i++) { rstart = array[i]; rend = rstart; while (array[i + 1] - array[i] == 1) { rend = array[i + 1]; // increment the index if the numbers sequential i++; } ranges

contenteditable div backspace and deleting text node problems

让人想犯罪 __ 提交于 2019-11-26 19:06:15
问题 There are so many issues with contenteditable divs and deleting html and/or non content editable content inside editable divs. Using an answer by the excellent Tim Down here: How to delete an HTML element inside a div with attribute contentEditable? Using Tim's code, the entire text node gets deleted. I need this to work like any textarea would, deleting character by character and just making sure html elements can be backspaced as well. I tried the following else if(node){ var index = node

Checking a table for time overlap?

旧城冷巷雨未停 提交于 2019-11-26 18:54:00
I have a MySQL table with the following fields: name starttime endtime starttime and endtime are MySQL TIME fields (not DATETIME ). I need a way to periodically "scan" the table to see if there are any overlaps in time ranges within the table. If there is an event from 10:00-11:00 and another from 10:30-11:30 , I want to be alerted of the presence of the time overlap. Nothing fancy really, all I want to know whether an overlap exists or not. I'm going to be using PHP to execute this. This is a query pattern for which I found the answer many years ago: SELECT * FROM mytable a JOIN mytable b on

Is there a need for range(len(a))?

落爺英雄遲暮 提交于 2019-11-26 18:49:01
问题 One frequently finds expressions of this type in python questions on SO. Either for just accessing all items of the iterable for i in range(len(a)): print(a[i]) Which is just a clumbersome way of writing: for e in a: print(e) Or for assigning to elements of the iterable: for i in range(len(a)): a[i] = a[i] * 2 Which should be the same as: for i, e in enumerate(a): a[i] = e * 2 # Or if it isn't too expensive to create a new iterable a = [e * 2 for e in a] Or for filtering over the indices: for

Is there a way in Matlab using the pseudo number generator to generate numbers within a specific range?

不打扰是莪最后的温柔 提交于 2019-11-26 18:36:51
问题 For example: round(7*rand(1,5)) Generates 5 numbers between 1 and 7 Is there a way to generate 5 random numbers between 5 and 7? Or an abstraction of that? 回答1: More generally: minInt = 5; maxInt = 7; numInts = 10; r = randi([minInt, maxInt],[1,numInts]) r = 6 7 7 7 6 5 5 5 7 5 回答2: First, if you are wanting to generate random integer values, it's better to use the function RANDI. Then it's just a matter of shifting and scaling the random numbers accordingly. The following should give you