‏إظهار الرسائل ذات التسميات multiple. إظهار كافة الرسائل
‏إظهار الرسائل ذات التسميات multiple. إظهار كافة الرسائل

الجمعة، 27 ديسمبر 2013

How can I get a single random number from multiple possible ranges?

Would you choose a random Range and then use Random.Next() on that?

No, because that would give numbers in shorter ranges more weight. For example, if one range contains a single number 42 and the other range contains 10,000 numbers, then 42 would be generated roughly 50% of the time.

Or would you keep choosing random numbers until you got one that was within each of the ranges?

No, because that would not be the too efficient. For example, if the first range is [1..3] and the second range is [200,000..200,001], getting a number in one of these ranges would take a while.

I would implement a Size property on the range, compute the total size, generate an int in the range index = [0..TotalSize-1], and then picked the item at the index as if all numbers in your ranges were numbered sequentially.

For example, in your ranges TotalSize would be 6+7+12=25. First, I would generate a random number in the range [0..24], say, 15. I would then see that 15 falls in the third range, so I'd return 21.

This would give each range a weight proportional to its size. If you would like to assign specific weights to your ranges, the algorithm would be different: you would compute an equivalent of TotalRange by multiplying the actual size by the weight of the range, and totaling up the products. You would then generate a number in the range of that weighted sum, pick the range by working backward from that random number, and then dividing out the weight of the specific range to obtain the position of the random item in the range.


View the original article here

الثلاثاء، 24 ديسمبر 2013

C++ Multiple Parameter Object creation

I am a virtual beginner in programming and am trying to understand a simple part of Object oriented programming. I apologize in advance if the answer to my question seems obvious, but I have tried to find similar examples online using google and youtube but sadly nothing fits the bill. With that said here is my question.

I am trying to make a simple video game inventory similar to the ones you would see in RPGs such as Final Fantasy VI or Chrono Trigger. What I am having trouble with is creating the items of the inventory. What I'd like help with is how to create one object that defines one item, for example a potion. I know it will take multiple parameters which are listed here:

itemText = "Potion";itemDesc = "Regain 50 HP";itemEffect = playerHP + 50;buyCost = 50;sellCost = 25;

What I'd like help with is syntax of the connection of these parameters in one object. Later I will use the data so that I can create an array that shows only the itemText and a Quantity for the player to see. Any and all help or places would be appreciated.

Thank you in advance, Delita


View the original article here

الجمعة، 20 ديسمبر 2013

Check multiple exceptions in Scala retry control structure

I'm trying to develop a control structure that retries on some declared exceptions but throw others. The control structure works nicely but I have problem checking if the exception caught belongs to the types of declared exceptions. In more generic words, how do I check if a parameter is one of the declared list of type parameters?

Define a retry control structure:

def retry[T, R](times:Int=3, delay:Int=1000)(ex:Class[_<:Exception]*)(t:T)(block:T=>R):R = {try { block(t)}catch { case e:Exception if (isOneOf(e, ex:_*) && times>0) => { println(s"Exception $e happened, sleep $delay milliseconds") Thread.sleep(delay) println(s"$times times remain for retry before give up") retry(times-1, delay)(ex:_*)(t)(block) } case e:Throwable => { println(s"Exception $e is not handled") throw e }}

}

define a function that throws multiple exceptions

def d(str:String) = { val r = Math.random() println(r) if (r>0.6) throw new IllegalArgumentException else if (r>0.4) throw new java.io.IOException else if (r>0.2) throw new UnsupportedOperationException else println(str)}

and I can call the function with retry as:

retry(3, 1000)(classOf[IllegalArgumentException], classOf[java.io.IOException])("abc"){ x=> d(x)}

I'm want to retry on IllegalArgumentException and IOException, but will throw UnsupportedOperationException.

For the retry structure, the list of declared exceptions is passed in dynamically at runtime. So multiple exception case statement does not work for me. When an exception is caught, I match it with generic exception, check the exception type with the isOneOf function. Ideally the function will take a sequence of types, instead of a sequence of classes. How do I pass in a sequence of exception types, rather than a sequence of classes and check the caught exception against the sequence of types?


View the original article here

Return same row multiple times if search string matches with 2 columns - Mysql

I'm trying to display the result from the following table

id | string1 | string2 1 | mysql | php 2 | mysql | mysql 3 | python | java

If I search the string 'mysql' in the table from column string1 and string2, I would like to get results as

1 mysql 2 mysql 2 mysql

Row id 2 should display multiple times as 'mysql' is presents in 2 columns(string1 and string2).

Is there any feasiblity to list like above results.

Note : I dont want to use UNION all

Thanks


View the original article here