Random number keeps changing












0















So I have a super simple flip the coin game, but it's multiplayer so there is a lot of code in it. In the flipping part of the game, I am having an issue.



I want it so that whenever the coin is flipped, if it lands on heads the number 1 gets stored into the Firebase database, and if it lands on tails I want the number 2 stored.



This is how I have it working right now:



private void startGame() {
flipCoin();
games.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
HomePage.getCurrentGameID(new HomePage.CallbackID() {
@Override
public void onSuccess(String currentGameID) {
games.child(currentGameID).child("horT").setValue(getCoinSide());
}
});
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}


Once both players join a game and hit 'ready', then this method is called. You can see that first it calls the flipCoin() method which is simply this:



private void flipCoin() {
final Random r = new Random();
coinSide = r.nextInt(2) + 1;
}


And then I am setting the heads or tails (the 1 or 2) in the database using getCoinSide() as you can see in this line that I've already posted



games.child(currentGameID).child("horT").setValue(getCoinSide());


And my getCoinSide() is simply this:



private int getCoinSide() {
return coinSide;
}


So this works somewhat. It gets a number and stores it in the database, but the problem is it continuously stores it in the database over and over and over. So the number that defines heads or tails keeps switching every half second to a 1 or a 2. How can I get a static number that has no chance of changing?










share|improve this question























  • Well, obviously if it the currentGameID already has a value set do not set it again, but then there's still issues like race conditions which means you'll need to start a transaction before testing and writing...

    – Ken Y-N
    Nov 20 '18 at 3:17











  • I am confused by your response. currentGameID is not being set here. I have one line of code posted two different times just to single out the important line

    – CoderKlipto
    Nov 20 '18 at 3:19
















0















So I have a super simple flip the coin game, but it's multiplayer so there is a lot of code in it. In the flipping part of the game, I am having an issue.



I want it so that whenever the coin is flipped, if it lands on heads the number 1 gets stored into the Firebase database, and if it lands on tails I want the number 2 stored.



This is how I have it working right now:



private void startGame() {
flipCoin();
games.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
HomePage.getCurrentGameID(new HomePage.CallbackID() {
@Override
public void onSuccess(String currentGameID) {
games.child(currentGameID).child("horT").setValue(getCoinSide());
}
});
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}


Once both players join a game and hit 'ready', then this method is called. You can see that first it calls the flipCoin() method which is simply this:



private void flipCoin() {
final Random r = new Random();
coinSide = r.nextInt(2) + 1;
}


And then I am setting the heads or tails (the 1 or 2) in the database using getCoinSide() as you can see in this line that I've already posted



games.child(currentGameID).child("horT").setValue(getCoinSide());


And my getCoinSide() is simply this:



private int getCoinSide() {
return coinSide;
}


So this works somewhat. It gets a number and stores it in the database, but the problem is it continuously stores it in the database over and over and over. So the number that defines heads or tails keeps switching every half second to a 1 or a 2. How can I get a static number that has no chance of changing?










share|improve this question























  • Well, obviously if it the currentGameID already has a value set do not set it again, but then there's still issues like race conditions which means you'll need to start a transaction before testing and writing...

    – Ken Y-N
    Nov 20 '18 at 3:17











  • I am confused by your response. currentGameID is not being set here. I have one line of code posted two different times just to single out the important line

    – CoderKlipto
    Nov 20 '18 at 3:19














0












0








0








So I have a super simple flip the coin game, but it's multiplayer so there is a lot of code in it. In the flipping part of the game, I am having an issue.



I want it so that whenever the coin is flipped, if it lands on heads the number 1 gets stored into the Firebase database, and if it lands on tails I want the number 2 stored.



This is how I have it working right now:



private void startGame() {
flipCoin();
games.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
HomePage.getCurrentGameID(new HomePage.CallbackID() {
@Override
public void onSuccess(String currentGameID) {
games.child(currentGameID).child("horT").setValue(getCoinSide());
}
});
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}


Once both players join a game and hit 'ready', then this method is called. You can see that first it calls the flipCoin() method which is simply this:



private void flipCoin() {
final Random r = new Random();
coinSide = r.nextInt(2) + 1;
}


And then I am setting the heads or tails (the 1 or 2) in the database using getCoinSide() as you can see in this line that I've already posted



games.child(currentGameID).child("horT").setValue(getCoinSide());


And my getCoinSide() is simply this:



private int getCoinSide() {
return coinSide;
}


So this works somewhat. It gets a number and stores it in the database, but the problem is it continuously stores it in the database over and over and over. So the number that defines heads or tails keeps switching every half second to a 1 or a 2. How can I get a static number that has no chance of changing?










share|improve this question














So I have a super simple flip the coin game, but it's multiplayer so there is a lot of code in it. In the flipping part of the game, I am having an issue.



I want it so that whenever the coin is flipped, if it lands on heads the number 1 gets stored into the Firebase database, and if it lands on tails I want the number 2 stored.



This is how I have it working right now:



private void startGame() {
flipCoin();
games.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
HomePage.getCurrentGameID(new HomePage.CallbackID() {
@Override
public void onSuccess(String currentGameID) {
games.child(currentGameID).child("horT").setValue(getCoinSide());
}
});
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}


Once both players join a game and hit 'ready', then this method is called. You can see that first it calls the flipCoin() method which is simply this:



private void flipCoin() {
final Random r = new Random();
coinSide = r.nextInt(2) + 1;
}


And then I am setting the heads or tails (the 1 or 2) in the database using getCoinSide() as you can see in this line that I've already posted



games.child(currentGameID).child("horT").setValue(getCoinSide());


And my getCoinSide() is simply this:



private int getCoinSide() {
return coinSide;
}


So this works somewhat. It gets a number and stores it in the database, but the problem is it continuously stores it in the database over and over and over. So the number that defines heads or tails keeps switching every half second to a 1 or a 2. How can I get a static number that has no chance of changing?







java android firebase firebase-realtime-database






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 3:11









CoderKliptoCoderKlipto

617




617













  • Well, obviously if it the currentGameID already has a value set do not set it again, but then there's still issues like race conditions which means you'll need to start a transaction before testing and writing...

    – Ken Y-N
    Nov 20 '18 at 3:17











  • I am confused by your response. currentGameID is not being set here. I have one line of code posted two different times just to single out the important line

    – CoderKlipto
    Nov 20 '18 at 3:19



















  • Well, obviously if it the currentGameID already has a value set do not set it again, but then there's still issues like race conditions which means you'll need to start a transaction before testing and writing...

    – Ken Y-N
    Nov 20 '18 at 3:17











  • I am confused by your response. currentGameID is not being set here. I have one line of code posted two different times just to single out the important line

    – CoderKlipto
    Nov 20 '18 at 3:19

















Well, obviously if it the currentGameID already has a value set do not set it again, but then there's still issues like race conditions which means you'll need to start a transaction before testing and writing...

– Ken Y-N
Nov 20 '18 at 3:17





Well, obviously if it the currentGameID already has a value set do not set it again, but then there's still issues like race conditions which means you'll need to start a transaction before testing and writing...

– Ken Y-N
Nov 20 '18 at 3:17













I am confused by your response. currentGameID is not being set here. I have one line of code posted two different times just to single out the important line

– CoderKlipto
Nov 20 '18 at 3:19





I am confused by your response. currentGameID is not being set here. I have one line of code posted two different times just to single out the important line

– CoderKlipto
Nov 20 '18 at 3:19












1 Answer
1






active

oldest

votes


















2














You've written an infinite loop. You have a listener at a location in the database (games) that gets invoked whenever any value at or under that location changes. Inside your listener, you have some code that writes a value to a location under games. That written value then triggers the listener you added at games, which writes another value there, etc.



You're going to have to figure out some other way to meet the requirements of your game without writing a self-triggering listener like this, perhaps by limiting the scope of your listener so that it doesn't also listen to the values it's writing.






share|improve this answer
























  • Thank you, I didn't realize this was looping infinitely. What I did to solve the problem was change addValueEventListener to addListenerForSingleValueEvent so that it would just get one value and then get out of the method. Thanks again

    – CoderKlipto
    Nov 20 '18 at 3:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385665%2frandom-number-keeps-changing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You've written an infinite loop. You have a listener at a location in the database (games) that gets invoked whenever any value at or under that location changes. Inside your listener, you have some code that writes a value to a location under games. That written value then triggers the listener you added at games, which writes another value there, etc.



You're going to have to figure out some other way to meet the requirements of your game without writing a self-triggering listener like this, perhaps by limiting the scope of your listener so that it doesn't also listen to the values it's writing.






share|improve this answer
























  • Thank you, I didn't realize this was looping infinitely. What I did to solve the problem was change addValueEventListener to addListenerForSingleValueEvent so that it would just get one value and then get out of the method. Thanks again

    – CoderKlipto
    Nov 20 '18 at 3:25
















2














You've written an infinite loop. You have a listener at a location in the database (games) that gets invoked whenever any value at or under that location changes. Inside your listener, you have some code that writes a value to a location under games. That written value then triggers the listener you added at games, which writes another value there, etc.



You're going to have to figure out some other way to meet the requirements of your game without writing a self-triggering listener like this, perhaps by limiting the scope of your listener so that it doesn't also listen to the values it's writing.






share|improve this answer
























  • Thank you, I didn't realize this was looping infinitely. What I did to solve the problem was change addValueEventListener to addListenerForSingleValueEvent so that it would just get one value and then get out of the method. Thanks again

    – CoderKlipto
    Nov 20 '18 at 3:25














2












2








2







You've written an infinite loop. You have a listener at a location in the database (games) that gets invoked whenever any value at or under that location changes. Inside your listener, you have some code that writes a value to a location under games. That written value then triggers the listener you added at games, which writes another value there, etc.



You're going to have to figure out some other way to meet the requirements of your game without writing a self-triggering listener like this, perhaps by limiting the scope of your listener so that it doesn't also listen to the values it's writing.






share|improve this answer













You've written an infinite loop. You have a listener at a location in the database (games) that gets invoked whenever any value at or under that location changes. Inside your listener, you have some code that writes a value to a location under games. That written value then triggers the listener you added at games, which writes another value there, etc.



You're going to have to figure out some other way to meet the requirements of your game without writing a self-triggering listener like this, perhaps by limiting the scope of your listener so that it doesn't also listen to the values it's writing.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 3:21









Doug StevensonDoug Stevenson

77k990111




77k990111













  • Thank you, I didn't realize this was looping infinitely. What I did to solve the problem was change addValueEventListener to addListenerForSingleValueEvent so that it would just get one value and then get out of the method. Thanks again

    – CoderKlipto
    Nov 20 '18 at 3:25



















  • Thank you, I didn't realize this was looping infinitely. What I did to solve the problem was change addValueEventListener to addListenerForSingleValueEvent so that it would just get one value and then get out of the method. Thanks again

    – CoderKlipto
    Nov 20 '18 at 3:25

















Thank you, I didn't realize this was looping infinitely. What I did to solve the problem was change addValueEventListener to addListenerForSingleValueEvent so that it would just get one value and then get out of the method. Thanks again

– CoderKlipto
Nov 20 '18 at 3:25





Thank you, I didn't realize this was looping infinitely. What I did to solve the problem was change addValueEventListener to addListenerForSingleValueEvent so that it would just get one value and then get out of the method. Thanks again

– CoderKlipto
Nov 20 '18 at 3:25




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385665%2frandom-number-keeps-changing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)