numbers

Scaling Random Bytes to Selected Integer Range

点点圈 提交于 2019-12-22 08:27:18
问题 I have a file of true random bytes. I want a function that returns a random integer in the range given by taking a byte from the file and scaling it. (is this the right word?) public int getInt(int l, int h) throws IOException { int m = (h - l) + 1; // number of ranges needed int r = 256 / m; // size of byte range int x = (r * m) - 1; // maximum allowable byte value int b; do { try { // get random byte from file b = ram.readUnsignedByte(); } catch (EOFException e) { // catch EOF, reset

adding zeros in front of a string

微笑、不失礼 提交于 2019-12-22 06:45:26
问题 I need your help. I'd like to create a function that would add some zeros in front of a number. The maximum numbers of digits that the total string should have is 6. Here are examples: 9 -> 000009 14 -> 000014 230 -> 000230 1459 -> 001459 21055 -> 021055 987632 -> 987632 (Do nothing, there's already 6 digits) 回答1: A simple one line solution without any loops Works with IE5-11, Firefox, Chrome, etc. Assumes integer input. function pad(n) { return ("000000" + n).slice(-6); } Run snippet to test

How to catch properly an SqlException: A transport-level error has occurred

不想你离开。 提交于 2019-12-22 05:32:11
问题 I am getting an SqlException in the logs of .NET 3.5 app, I am looking for the corresponding number (value of the property SqlException.Number ). System.Data.SqlClient.SqlException: A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The specified network name is no longer available.) at System.Data.SqlClient.SqlConnection.OnError I am also getting error such as: System.Data.SqlClient.SqlException: A network-related or instance

convert number into words

拟墨画扇 提交于 2019-12-22 05:02:20
问题 even though this question has been posted and answered before. I wanted help with my code. Task is to convert a number into words from 0 to 10 million. I have tried to do that with my code using GUI, problem is it doesn't return an answer at all. Can anyone help me in identifying what the problem on the code could possibly be. code below: private void btnConvertToText_Click(object sender, EventArgs e) { string ConvertedNumber = " "; int number = Convert.ToInt32(txtNumber.Text); int Count = 0;

Java and C# - byte array to long conversion difference

China☆狼群 提交于 2019-12-22 04:53:08
问题 This is strange to me: when I run in Java byte[] data = new byte[] { 50, -106, 40, -22, -94, -119, -52, 8 }; ByteBuffer bb = ByteBuffer.wrap( data ); System.out.println( bb.getLong() ); result is 3645145936617393160 when I run in C# //unsigned values (signed&0xff) byte[] bytes = new byte[] { 50, 150, 40, 234, 162, 137, 204, 8 }; long l = BitConverter.ToInt64(bytes, 0); System.Console.Write(String.Format("{0}\n", l)); System.Console.ReadKey(); result is 634032980358633010 Can you help me to

Flexible numeric string parsing in Python

╄→尐↘猪︶ㄣ 提交于 2019-12-22 04:48:15
问题 Are there any Python libraries that help parse and validate numeric strings beyond what is supported by the built-in float() function? For example, in addition to simple numbers (1234.56) and scientific notation (3.2e15), I would like to be able to parse formats like: Numbers with commas: 2,147,483,647 Named large numbers: 5.5 billion Fractions: 1/4 I did a bit of searching and could not find anything, though I would be surprised if such a library did not already exist. 回答1: If you want to

regular expression to add characters before and after numbers

我怕爱的太早我们不能终老 提交于 2019-12-22 03:48:25
问题 I have a list of numbers between square brackets, and I need to add words before and after the exact numbers (i.e. keep the same numbers). I use notepad++ to replace, but if you have a solution with other program please advise. Example: text [121] othertext moretext [16] othertextmore andtext [5940] othertextplus outcome: text xxxxxxxxx [121] xxxxxxxxx othertext moretext xxxxxxxxx [16] xxxxxxxxx othertextmore andtext xxxxxxxxx [5940] xxxxxxxxx othertextplus The numbers are of course \d+ but I

Storing multiple values in a single cell in MySQL

三世轮回 提交于 2019-12-22 01:22:19
问题 I'm storing multiple numbers in a MySQL cell by using a delimiter (eg "1,5,10") and the TEXT datatype. How do I perform lookups like this? SELECT * FROM MyTable WHERE MultiVals CONTAINS "5" And lastly, is this the preferred approach to store such values? I'm using this like a linker table, to link certain rows to multiple other rows in another table (via IDs). Since I'm trying to minimize the filesize of the DB, I thought this would be a more compact approach to linking instead of using

Creating half a number pyramid in PHP with for loops

瘦欲@ 提交于 2019-12-22 01:16:27
问题 I was told to create half a pyramid in for loops using php that looks as follows: 54321 4321 321 21 1 This is what I had created so far but I have no idea how to go about creating the rest of the pyramid. for ($iB = 5; $iB >=0; $iB--) { echo $iB.'<br>'; } 回答1: Use a nested for loop . Try this... for ($iB = 5; $iB > 0; $iB--) { for($i = $iB; $i > 0; $i--){ echo $i; } echo '<br />'; } 回答2: In case someone decides not to use numbers for input, do $str = '54321'; while( $str ) { echo $str . PHP

How to Convert from a Residual Number System to a Mixed Radix System?

冷暖自知 提交于 2019-12-21 20:48:28
问题 I understand the concept of a Residual Number System and the concept of a Mixed Radix system, but I'm having difficulty getting any of the conversion methods I find to work in a simple case study. I started at Knuth's Art of Computer Programming but that had a bit too much on the theory of the conversion, and once Euler was mentioned I was lost. Wikipedia has a nice section on the subject, which I tried here and here but both times I couldn't get back to the number where I started. I found a