Jump to content

Rng Problem


ChidoriSoul
 Share

Recommended Posts

My random number generator does not work -.-

<script type="text/javascript">function random_catch(){var random_number=Math.floor(Math.random()*8);if (random_number >= 0 && random_number <= 10)	 document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=16";else if (random_number >= 0 + 1 && random_number <= 10)	 document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=9";else if (random_number >= 0 + 1 && random_number <= 10)	 document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=14";else	 document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=15";return false;}</script><select id="pokemon_select" name="pokemon"><option value="Continue">Proceed</option></select><input type="submit" value="Search!" onclick="return random_catch();" />

I can only find the last option, id=15

Link to comment
Share on other sites

Every time you have an if, else-if, or else statement, you need opening and closing braces {} around the lines that you want to run in the event of that condition. Try adding them in all the relevant places and see what happens.Edit: Also, your conditions don't seem to be right. You have two that are the same...

Edited by chibineku
Link to comment
Share on other sites

I still only find the 4th options, here is my new partvar random_number=Math.floor(Math.random()*12);if (random_number >= 0 && random_number <= 10) {document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=16";}else if (random_number >= 0 + 1 && random_number <= 10) {document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=9";}else if (random_number >= 0 + 1 && random_number <= 10) {document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=14";}else {document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=15";}Also, what do you mean by the same>

Edited by ChidoriSoul
Link to comment
Share on other sites

Well, the first option in your list of ifs catches almost all the results, because page 16 shows for any number between 0 and 10, and you're only rolling out of 12. I added an alert to see what numbers it's throwing out, and they are as random as you like. What you need is for each condition to be narrower. For example:

<script type="text/javascript">function random_catch() {var random_number=Math.floor(Math.random()*12);if (random_number >= 0 && random_number <= 3){	 document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=16";} else if (random_number > 3 && random_number <= 6) {	 document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=9";} else if (random_number > 6 && random_number <= 9) {	 document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=14";} else {	 document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=15";}return false;}</script>

And what I meant before about two of your conditions being the same is that you had put:

else if (random_number >= 0 + 1 && random_number <= 10){document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=9";}else if (random_number >= 0 + 1 && random_number <= 10){document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=14";}

Both of those conditions are to trip if the number is >= 0+1 and <= 10. You'll never get the second one to go because the first will always fire given a matching number.

Link to comment
Share on other sites

yeah, but with this code, I always find any of them in 1 click, how do I make it so the chance for each page, except 16 (the try again page) has a 1/10 chance?
You need to have 10 pages and a number generator with a range 1-10, or get your hands dirty doing some jiggery-pokery with actuarial maths. For 1/10 odds, you want to generate a random number between 1 and 30 and then arbitrarily discard 9 numbers per if/else if clause. For example:if (random_number==2) { first page}else if (random_number==15) {second page}else if (random_number==27){third page}else {try again page}If you don't want to choose arbitrary numbers to match for each band of 10, you could always have another random number generated for each condition, so the first would choose between 1 and 10 and see if they match:var random_number=Math.floor(Math.random()*30)var random_number1=Math.floor(Math.random()*10)var random_number2=Math.round((20-10) * Math.random() + 10)var random_number3=Math.round((30-20) * Math.random() + 20)if (random_number==random_number1) {first page} etc. etc.Then for everything else, you get the try again page.Edit: ignore the above...that actually gives statistical odds of 1/100, because the chance of generating a given number at random from 10 is 1/10, and the changes of getting the same number again is 1/10 * 1/10... Anyway, that's neither here nor there since you aren't using that solution. Edited by chibineku
Link to comment
Share on other sites

Yeah, what you say makes no sense to me, and this is different than what I was told to do, -.-This is a incorrect code, where I can only find the very last option, I just want to make it how to find all 4 of them.

<script type="text/javascript">function random_catch(){var random_number=Math.floor(Math.random()*502);if (random_number >= 0 && random_number <= 500)	 document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=18";else if (random_number >= 0 + 1 && random_number <= 500)	 document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=16";else if (random_number >= 0 + 1 && random_number <= 500)	 document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=15";else if (random_number >= 0 + 1 && random_number <= 500)	 document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=13";else	 document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=17";return false;}</script><select id="pokemon_select" name="pokemon"><option value="Continue">Proceed</option></select><input type="submit" value="Search!" onclick="return random_catch();" />

Link to comment
Share on other sites

if (random_number >= 0 && random_number <= 500) document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=18";else if (random_number >= 0 + 1 && random_number <= 500) document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=16";else if (random_number >= 0 + 1 && random_number <= 500) document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=15";else if (random_number >= 0 + 1 && random_number <= 500) document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=13";else document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=17";
All your conditions are the same, so you'll only ever get page 18 or the default page 17. They have to have different ranges, they can't all be between 0/1 and 500.
Link to comment
Share on other sites

so, it could be like this

if (random_number >= 0 && random_number <= 500)document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=18";else if (random_number >= 0 + 1 && random_number <= 500)document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=16";else if (random_number >= 0 + 2 && random_number <= 500)document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=15";else if (random_number >= 0 + 3 && random_number <= 500)document.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=13";elsedocument.location = "http://www.punbb-hosting.com/forums/FlameValley/page.php?id=17";

Would that work?

Link to comment
Share on other sites

If your script were to generate a number between 0 and 500, the first condition will always be true and you'll get page 18. Why? Because you are checking to see if the number is greater than or equal to 0 and less than or equal to 500. Since your numbers are between 0 and 500, that will always be true.This is more like what you're talking about:

if(random_number >= 0 && random_number <= 9)else if(random_number >= 10 && random_number <= 19)else if(random_number >= 20 && random_number <= 29)else if(random_number >= 30 && random_number <= 39)...else if(random_number >= 490 && random_number <= 500)

Link to comment
Share on other sites

I believe you're looking for something like this

var random_number=Math.floor(Math.random()*8);switch(random_number) {  case 0:  window.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=16";  break;  case 1:  window.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=9";  break;  case 3:  window.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=14";  break;  case 4:  document.location = "http://www.punbb-hosting.com/forums/the_other_tppc_TC/page.php?id=15";  break;  case 5:  // . . .  // The rest of the cases up to 10  // . . .  default:  alert("Try again");}

Edited by Ingolme
Link to comment
Share on other sites

  • 2 weeks later...

So, what is wrong with this code

<script type="text/javascript">function random_catch(){var random_number=Math.floor(Math.random()*30);if (random_number==2) {http://www.punbb-hosting.com/forums/AquaLegend/page.php?id=1};else if (random_number==15) {http://www.punbb-hosting.com/forums/AquaLegend/page.php?id=2};else if (random_number==27) {http://www.punbb-hosting.com/forums/AquaLegend/page.php?id=3};else {http://www.punbb-hosting.com/forums/AquaLegend/page.php?id=4};return false;}</script><select id="pokemon_select" name="pokemon"><option value="Continue">Proceed</option></select><input type="submit" value="Search!" onclick="return random_catch();" />

Link to comment
Share on other sites

So, what is wrong with this code
You're not assigning to window.location (and you forgot quotes).
if (random_number == 2) window.location = "http://www.punbb-hosting.com/forums/AquaLegend/page.php?id=1";else if (random_number == 15) window.location = "http://www.punbb-hosting.com/forums/AquaLegend/page.php?id=2";else if (random_number == 27) window.location = "http://www.punbb-hosting.com/forums/AquaLegend/page.php?id=3";else window.location = "http://www.punbb-hosting.com/forums/AquaLegend/page.php?id=4";

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...