Is there anyway to random object with percentage?
For example: i have an CCArray with 3 CCSprite: spriteA, spriteB, spriteC.
spriteA = 30% chance to appear, spriteB = 60% chance to appear, spriteC = 10% chance to appear.
I have try this but it seem wrong:
int r = arc4random() % 100;
if (r >= 0 && r < 10)
{
CCLOG("spriteC appear");
}
else if (r >= 10 && r < 30)
{
CCLOG("spriteA appear");
}
else if (r >= 30 && r < 100)
{
CCLOG("spriteB appear");
}
Related
This piece of code is effective but I don't feel good about it when I read it. I'm finding trouble refactoring into something more legible or short. Breaking the code into several if-return doesn't seem to be of great help here. Tips? Thanks!
if t < 0 {
r = 0
} else if t >= 0 && t < 7.5 {
if m >= 0 && m < 0.125 {
r = 1
} else {
r = 2
}
} else if t >= 7.5 && t < 10 {
if m >= 0 && m < 0.125 {
r = 1
} else {
r = 4
}
} else if t >= 10 && t < 20 {
if m >= 0 && m < 0.125 {
r = 1
} else if m >= 0.125 && m < 0.25 {
r = 3
} else {
r = 4
}
} else if t >= 20 && t < 22.5 {
if m >= 0 && m < 0.125 {
r = 7
} else if m >= 0.125 && m < 0.375 {
r = 3
} else {
r = 4
}
} else if t >= 22.5 && t < 27.5 {
if m >= 0 && m < 0.125 {
r = 7
} else if m >= 0.125 && m < 0.625 {
r = 5
} else {
r = 6
}
} else if t >= 27.5 {
if m >= 0 && m < 0.25 {
r = 7
} else if m >= 0.25 && m < 0.625 {
r = 5
} else {
r = 6
}
}
Simplify the outer if / else if / else removing redundant comparisons and converting to switch:
switch {
case t < 0:
r = 0
case t < 7.5:
if m >= 0 && m < 0.125 {
r = 1
} else {
r = 2
}
case t < 10:
if m >= 0 && m < 0.125 {
r = 1
} else {
r = 4
}
case t < 20:
if m >= 0 && m < 0.125 {
r = 1
} else if m >= 0.125 && m < 0.25 {
r = 3
} else {
r = 4
}
case t < 22.5:
if m >= 0 && m < 0.125 {
r = 7
} else if m >= 0.125 && m < 0.375 {
r = 3
} else {
r = 4
}
case t < 27.5:
if m >= 0 && m < 0.125 {
r = 7
} else if m >= 0.125 && m < 0.625 {
r = 5
} else {
r = 6
}
default:
if m >= 0 && m < 0.25 {
r = 7
} else if m >= 0.25 && m < 0.625 {
r = 5
} else {
r = 6
}
}
Similar modifications are possible on the inner statements. The code:
if m >= 0 && m < 0.125 {
r = 1
} else {
r = 2
}
becomes:
switch {
case m < 0:
// r not changed
case m < 0.125:
r = 1
default:
r = 2
}
Looking at the values, you can encode the range checks with some data structures:
type Rng struct {
From,To float64
V int
}
func (r Rng) In(val float64) bool { return val>=r.From && val<r.To }
type ARange struct {
T Rng
M []Rng
Def int // Default value for r if none of the M ranges match
}
var ranges=[]ARange{ {T:Rng{0,7.5}, M:[]Rng{ {0,0.125,1} }, Def: 2 },
...
Then, write a for-loop that goes through each element of ranges and checks t and m to see if there are any matches.
I was reading Spark correlation algorithm source code and while going through the code, I coulddn't understand this particular peace of code.
This is from the file : org/apache/spark/mllib/linalg/BLAS.scala
def spr(alpha: Double, v: Vector, U: Array[Double]): Unit = {
val n = v.size
v match {
case DenseVector(values) =>
NativeBLAS.dspr("U", n, alpha, values, 1, U)
case SparseVector(size, indices, values) =>
val nnz = indices.length
var colStartIdx = 0
var prevCol = 0
var col = 0
var j = 0
var i = 0
var av = 0.0
while (j < nnz) {
col = indices(j)
// Skip empty columns.
colStartIdx += (col - prevCol) * (col + prevCol + 1) / 2
av = alpha * values(j)
i = 0
while (i <= j) {
U(colStartIdx + indices(i)) += av * values(i)
i += 1
}
j += 1
prevCol = col
}
}
}
I do not know Scala and that could be the reason I could not understand it. Can someone explain what is happening here.
It is being called from Rowmatrix.scala
def computeGramianMatrix(): Matrix = {
val n = numCols().toInt
checkNumColumns(n)
// Computes n*(n+1)/2, avoiding overflow in the multiplication.
// This succeeds when n <= 65535, which is checked above
val nt = if (n % 2 == 0) ((n / 2) * (n + 1)) else (n * ((n + 1) / 2))
// Compute the upper triangular part of the gram matrix.
val GU = rows.treeAggregate(new BDV[Double](nt))(
seqOp = (U, v) => {
BLAS.spr(1.0, v, U.data)
U
}, combOp = (U1, U2) => U1 += U2)
RowMatrix.triuToFull(n, GU.data)
}
The correlation is defined here:
https://en.wikipedia.org/wiki/Pearson_correlation_coefficient
The final goal is to understand the Spark correlation algorithm.
Update 1: Relevent paper https://stanford.edu/~rezab/papers/linalg.pdf
if ( x < 275 && x >= 0 )
f = 275
else if ( x < 450 && x >= 275) (right side comparison always previous left side default comparison value
f = 450
else if ( x < 700 && x >= 450)
f = 700
else if ( x < 1000 && x >= 700)
f = 1000
..... and more
is there any way or mathematical formula approach to eliminate this multiple if else statement for less code require?
Here is something in C (but easy enough to port to almost anything). It misses a couple of conditions (x < 0 and x > "biggest known") It doesn't add much value when you only have 4 values, but the more values you have, the more code this removes. Note that it will slow things down, but doubtful that you'd notice it unless you had a huge list of possible values.
int getF(int x)
{
/* possibleValues must be sorted in ascending order */
int possibleValues[] = { 275, 450, 700, 1000 };
for(int i = 0; i < sizeof(possibleValues) / sizeof(possibleValues[0]); i++)
{
int pv = possibleValues[i];
if (x < pv)
{
return pv;
}
}
/* fall thru "error" handle as you see fit */
return x;
}
if(x>0)
{
if(x<275)
f = 275;
else if(x<450)
f = 450;
else if(x<700)
f = 700;
else if(x<1000)
f = 1000;
else //and so on
Assuming that x>0. Just avoiding if-else, this also shall suffice the conditions -
x<275 ? 275 : (x<450 ? 450 : (x<700 ? 700 : (x<1000 ? 1000 :
Though if there is any defined type range of inputs like INT, BIG etc instead of 275, 450.. you can check the type of input. Also you can so the same using iteration as suggested by #John3136.
How do I check how many numbers from 1 to N (N < 100) have number 3 in it without converting it to a string to check it?
You can use th % mod operator to take the digits of the number one by one, and check them with 3.
Like,
int x;
while(num != 0) // num here goes from 1 to 100
{
x = num % 10;
if(x == 3)
{
//eureka
}
num /= 10;
}
EDIT
Lets check the algorithm for 35.
First iteration
//num = 35
x = num % 10; // x = 5 (35 % 10)
if(x == 3) // is x equal to 3 (NO)
{
//eureka
}
num /= 10; // num = 3 (35 / 10)
While loop check
num != 0 // num = 5
Second Iteration
//num = 35
x = num % 10; // x = 3 (5 % 10)
if(x == 3) // is x equal to 3 (YES)
{
//eureka
}
num /= 10; // num = 0 (5 / 10)
While loop check
num != 0 // num = 0
// While loop exits
I think the simplest way is by remainder and checking if number is between 30 and 39
if((x%10)==3||(x<40&&x>=30))
{
//Thats it
}
You can make use of the modulas operator % such that if(n % 3 == 1){success operation}
I basically have a few variables
0 < na < 250
0 < max <= 16
nb = (na + max - 1) / max
n has the following characterstics
0 <= i < nb - 1 => n = max
i = nb - 1 => n = na - i * max
Is there an easy way to do this without the ternary operator?
for (i = 0; i<nb;i++) {
n = ((i + 1) * max > na ? na - (i * max) : max);
}
Examples
na = 5
max = 2
nb = 3
i = 0 => n = 2
i = 1 => n = 2
i = 2 => n = 1
na = 16
max = 4
nb = 4
i = 0 => n = 4
i = 1 => n = 4
i = 2 => n = 4
i = 3 => n = 4
na = 11
max = 3
nb = 4
i = 0 => n = 3
i = 1 => n = 3
i = 2 => n = 3
i = 3 => n = 2
The question is not very clear. Perhaps you're looking for something like this:
for (i=0;i < nb;++i)
{
n = i < nb - 1 ? max : (na - 1) % max + 1;
}
You don't need to calculate nb. This is one way you could do it (C#):
int na = 11;
int max = 4;
for (int i = 0, x = 0; x < na; i++, x += max)
{
int n = Math.Min(max, na - x);
Console.WriteLine("i = {0}, n = {1}", i, n);
}
Output:
i = 0, n = 4
i = 1, n = 4
i = 2, n = 3
Just to add more confusion to the thread:
If only you print max in the first two cases, then you could do something like: (not in any particular language)
//for 0
printf("i = %d, n = %d\n",i,max)
//for 1
printf("i = %d, n = %d\n",i,max)
//for the rest
for (i = 2; i<nb;i++) {
printf("i = %d, n = %d\n",i,na - (i * max));
}
You can avoid the operator doing two for loops
for (i = 0; (i + 1) * max) > na AND i < nb;i++) {
printf("i = %d, n = %d\n",i,0);
}
for (; i<nb;i++) {
printf("i = %d, n = %d\n",i,na - (i * max));
}