mardi 25 août 2015

Return to Last Opened Activity with Some Condition

i have a few interesting question here about how to return to latest activity if the game was restart because i have a new game button and continue button . so when continue button clicked it will return to last activity that opened before and the condition is activity is random from activityone to activityfive

i will explain with my code

this is menu.class

public class menu extends Activity {

int level;

Button newgame, continues, continuelocked;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.menu);

    continuelocked=(Button)findViewById(R.id.buttoncontinuelocked);

    continues=(Button)findViewById(R.id.buttoncontinue);

    newgame=(Button)findViewById(R.id.buttonnewgame);
    newgame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){

            Intent i =new Intent(menu.this, intro.class);
            startActivity(i);          
            }             
      });
}           

public void onResume() {
    super.onResume();

       SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE); 
       level = pref.getInt("Level", 0); 

       if(level == 0)

        {   
           continuelocked.setVisibility(View.VISIBLE);
           continues.setVisibility(View.GONE);
        }   

       if(level == 1)

        {   
           continuelocked.setVisibility(View.GONE);
           continues.setVisibility(View.VISIBLE);
        }          

           SharedPreferences.Editor editor = pref.edit();
           editor.putInt("Level", level);
           editor.commit();

           continues=(Button)findViewById(R.id.buttoncontinue);
           continues.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v){

         //How to set this method to return to latest activity that i play before
         //if i use random levelactivity?

              });
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
  }
}

and in intro.class i do this method to make activity random, check my code below here

@Override
public void onClick(View v) {
  // TODO Auto-generated method stub            

    button5 = (Button)findViewById(R.id.button5);
    if(v==button5) {

        SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE); 
        SharedPreferences.Editor editor = pref.edit();      
        editor.putInt("Level", 1);  
        editor.commit();                     

     // Here, we are generating a random number
     Random generator = new Random();
     int number = generator.nextInt(5) + 1; 
     // The '5' is the number of activities

     Class activity = null;

     // Here, we are checking to see what the output of the random was
     switch(number) { 
         case 1:
             // E.g., if the output is 1, the activity we will open is ActivityOne.class
             activity = ActivityOne.class;
             break;
         case 2:
             activity = ActivityTwo.class;
             break;
         case 3:
             activity = ActivityThree.class;
             break;
         case 4:
             activity = ActivityFour.class;
             break;
         default:
             activity = ActivityFive.class;
             break;
     }
     // We use intents to start activities
     Intent intent = new Intent(getBaseContext(), activity);
     startActivity(intent);
   }

and in every Activity "One to Five" i put the same random activity code

@Override
    public void onClick(View v) {
        // Here, we are generating a random number
        Random generator = new Random();
        int number = generator.nextInt(5) + 1; 
        // The '5' is the number of activities

    Class activity = null;

    // Here, we are checking to see what the output of the random was
    switch(number) { 
        case 1:
            // E.g., if the output is 1, the activity we will open is ActivityOne.class
            activity = ActivityOne.class;
            break;
        case 2:
            activity = ActivityTwo.class;
            break;
        case 3:
            activity = ActivityThree.class;
            break;
        case 4:
            activity = ActivityFour.class;
            break;
        default:
            activity = ActivityFive.class;
            break;
    }
    // We use intents to start activities
    Intent intent = new Intent(getBaseContext(), activity);
    startActivity(intent);
}
}

So My question is

First. How to open the last Activity with a Continue button if Activity was Random?

Second. if in every Activity had a same Random code to One until Five, How to set Disabled to Activity that already Opened before?

Anyone can explain about this?

Thank you in advance, have a good day




Better way to use parseInt() to change users' promt() into numbers

I set up a function to ask users a range of numbers, and gave them back a random number. The first try is okay.

// function settings
function getRandom(lower, upper) {
  return Math.floor(Math.random()*(upper-lower+1))+lower;
}

// ask users to give a rang of numbers
var lowerNum = parseInt(prompt("lower number?"));
var upperNum = parseInt(prompt("upper number?"));

document.write(getRandom(lowerNum, upperNum));

However, I thought it would be better to take parseInt() inside the function, so that it would be more clear and semantic.
// function settings
function getRandom(lower, upper) {
  lower = parseInt(lower);
  upper = parseInt(upper);
  return Math.floor(Math.random()*(upper-lower+1))+lower;
}

// ask users to give a rang of numbers
var lowerNum = prompt("lower number?");
var upperNum = prompt("upper number?");

document.write(getRandom(lowerNum, upperNum));

Both blocks of codes can do well and get the same results. But which one is the better practice, or performance when running the program?




Estimation of random versus fixed effect size in mixed models in r

I am analyzing an effect of food deprivation on bird chicks' calls. An idea is to show that food deprivations (experiment) contributes much more to acoustic parameter changes than species or individual identity. We have 3 species, 8 individuals of each species. A reviewer of our ms said that we should use mixed models with experiment and species as fixed and individual as random variables. I would like to rank a contribution of all 3 variables. How do I do it? I get a variance which random intercept adds to a population intercept, but it's not quite what I need. Also, anova(model_name) gives me F-values for both fixed effects but not for a random one. A paper by Nakagawa & Schielzeth (2013) is talking about R-sq. for random vs fixed effects, but I have two fixed variables and would like to rank all three. Any thoughts? Thank you! Here is the code (a model with a random intercept works better than intercept and slope).

m_Fmax <- lme(F_max ~ Experiment + Species +  Experiment*Species, random=~1|chicks, method = 'REML')




AND (&&) operator not working as expected in SWIFT

Im trying to randomly generate nodes on the screen without overlapping so I decided to find the range/distance of each node from each other and continuously randomly generate a position where the range from each node is greater than 100, once the range is over 100 it'll break out of the while loop and add the node to the view

    var p2Dp1: CGFloat = 0
    var p3Dp1: CGFloat = 0
    var p3Dp2: CGFloat = 0

    planet1.position = CGPoint(x: Int(arc4random_uniform(220) + 80), y: Int(arc4random_uniform(500) + 100))

    while p2Dp1 < 100.0 {

        planet2.position = CGPoint(x: Int(arc4random_uniform(220) + 80), y: Int(arc4random_uniform(500) + 100))
        p2Dp1 = SDistanceBetweenPoints(planet2.position, p2: planet1.position)
        print(" planet 2 distance from 1: \(p2Dp1) ")

    }

    while p3Dp1 < 100.0 && p3Dp2 < 100.0 {

        planet3.position = CGPoint(x: Int(arc4random_uniform(220) + 80), y: Int(arc4random_uniform(500) + 100))
        p3Dp1 = SDistanceBetweenPoints(planet3.position, p2: planet1.position)
        print(" planet 3 distance from 1: \(p3Dp1) ")
        p3Dp2 = SDistanceBetweenPoints(planet3.position, p2: planet2.position)
        print(" planet 3 distance from 2: \(p3Dp2) ")

    }

    self.addChild(planet1)
    self.addChild(planet2)
    self.addChild(planet3)

it starts by randomly giving the first node(planet1) a position on the view and then randomly give node2(planet2) a position where the distance from planet 1 and 2 are greater than 100. The first while loops seems to be working fine, its the second while loops thats giving me trouble. According to the console, the "&&" operator doesn't seem to be working

Console/Log:

planet 2 distance from 1: 96.1665222413705 planet 2 distance from 1: 185.407658957229 planet 3 distance from 1: 136.30847369111 planet 3 distance from 2: 55.1724568965349 EWWWW WE'RE TOUCHING

I set up the didBeginContact(contact: SKPhysicsContact) where it prints out if contact has happened

this is the method im using to find the distance:

func SDistanceBetweenPoints(p1: CGPoint, p2: CGPoint) -> CGFloat {

    return hypot(p2.x - p1.x, p2.y - p1.y);
}

can someone please tell me what I might be doing wrong? I'm planning to add 5 planets in total but just for now im testing it out with 3




Generate 4 random letters, then a random number between 0-9, then four more random letters, and one more random number between 0-9 in java script

I am looking to generate a coupon code, but i need to attach a string of 4 random letters, then a random number between 0-9, then four more random letters, and one more random number between 0-9 on to a base of NONO15-....i cannot figure out what this string would be.




Generating a Non-Repeating Random Number - PHP

I'm trying to get a custom function in php to return a random number between 1 and 20 that does not repeat i.e. produce the same number more than once, since I need to subsequently use this number to navigate to one of twenty web pages, and I don't want the same web page displayed.

Here is my code in three steps:

<form action="rand.php">
        <p>Click this button to display a random number that does not repeat...</p>
        <p><input type="submit" value="Generate"></p>
    </form>

Here is rand.php:

require_once('functions.php');

$page = generateNumber();
echo $page;

Here is functions.php:

<?php

$check = array();

function generateNumber() {

    global $check;

    $page_no = mt_rand(1,20);
    $check[] = $page_no;

    if (count($check) != 1) {

        foreach ($check as $val) {
            if ($val == $page_no) {
                $page_no = mt_rand(1,10);
                continue;
            }
        }
        return $page_no;
    }
    else {
        return $page_no;
    }
}

?>

My code seem to be functioning, however, it is repeating numbers so I am obviously doing something wrong. The reason I initially check the count is so that is returns the first number regardless, since it would be a single fresh number.

In order to see the number change I have been refreshing the rand.php page in my browser.




I need to generate a group of random numbers between 97 and 122

I have to generate random numbers between 97 and 122 using a specific algorithm but I get numbers that are greater than 122. Here is the code:

srand( time (NULL));

int num;
unsigned i;

for (i = 0;i<100;i++){

    num = 97+(rand()%122);

        printf("%d\n",num );

}

return 0;