Welcome Guest [Log In] [Register]
This board has been archived and is no longer accepting new questions. If you have a support question for your ZetaBoard, please visit us at the new support board. Registration is free and easy.


Visit the NEW ZetaBoards Support forum!

Username:   Password:
Add Reply
How do I make this random?
Topic Started: Jun 21 2008, 12:08 PM (624 Views)
Thunder
Member Avatar
Thunder
[ *  *  * ]
I'm creating this game where you can pick a number between 1 and 10, but if someone guesses the right number, they'll always know it. How do I make it so that it will choose a different number everytime?

Here's the code:

Code:
 
<script type="text/javascript">
//pick a number between 1 and 10
var x=prompt("Enter a number between 1 and 10:","");
try
{
if(x>10)
{
throw "Err1";
}
else if(x<0)
{
throw "Err2";
}
else if(isNaN(x))
{
throw "Err3";
}
else if(x<4 && x>0)
{
throw "Err5";
}
else if(x>=5 && x<6)
{
throw "Err4";
}
else if(x>5 && x<11)
{
throw "Err6";
}
}
catch(er)
{
if(er=="Err1")
{
alert("Sorry, that's to high");
}
if(er=="Err2")
{
alert("Sorry, that's to low");
}
if(er=="Err3")
{
alert("Error! The value is not a number");
}
if(er=="Err5")
{
alert("Try again");
}
if(er=="Err4")
{
alert("good job!");
}
if(er=="Err6")
{
alert("Try again");
}
}
</script>

Offline Profile Quote Post Goto Top
 
Fission
Member Avatar
I am really awesome
[ *  *  * ]
Google is awesome.

Code:
 

Math.round(Math.random()*9+1)


You could probably find a better solution than that somewhere on Google, but that is what I could find.
Offline Profile Quote Post Goto Top
 
WeirdAdz
Member Avatar
You cant go through life without saying something is Weird.
[ *  * ]
Code:
 
<script type="javascript">

$("td:contains('Display emoticons in your post')").append("<br /><input type='checkbox' onclick='Dice_Is(this.checked)'> Enable Attack?");

function Dice_Is(really){
if(really==true){
document.forms['posting'].onsubmit=function(){
document.forms['posting'].post.value+="\n\n\Your Attack did [b]"+Math.floor(Math.random()*30)+"[/b] Damage!";
}
}
}
</script>


This is what i use which is fairly similar.
Its Javascript. But you could take out parts which arent needed.
The code basically allows the member to use the option of using the Random feature, and after they have posted the number comes up inbetween the text either side 'Your attack did' & 'damage!'

Code:
 
Math.floor(Math.random()*30)

Change the Number to the maximum number.
Edited by WeirdAdz, Jun 21 2008, 01:17 PM.
Offline Profile Quote Post Goto Top
 
Fission
Member Avatar
I am really awesome
[ *  *  * ]
The basic formula for getting a range is Math.random()*(max-min)+min. And, if you want a whole number, us Math.floor, Math.round, etc. to round it.
Offline Profile Quote Post Goto Top
 
WeirdAdz
Member Avatar
You cant go through life without saying something is Weird.
[ *  * ]
Fission.. you don't need a minimum number unless you want it from 10-20, Also the code which i posted is already Whole Numbers, no decimals etc.
Offline Profile Quote Post Goto Top
 
Thunder
Member Avatar
Thunder
[ *  *  * ]
I want to know who's right. All I want is place to put part of a code that makes it do a random number between 1 and 10.

Fission
 
Code:
 
Math.round(Math.random()*9+1)


Where would I put this?
Offline Profile Quote Post Goto Top
 
Viral
Member Avatar
i'm stanley nao
[ *  *  *  *  * ]
They're both right, just Fission's example allows the range to be between y and x, not 1 and [x.

You can put that wherever you want.
Offline Profile Quote Post Goto Top
 
WeirdAdz
Member Avatar
You cant go through life without saying something is Weird.
[ *  * ]
Fission's method is useless if you are going from 1 - another number.
However it is great for when you are trying to go from say number 5 to number 8.

My method just goes from number 1 - another number.
Offline Profile Quote Post Goto Top
 
Thunder
Member Avatar
Thunder
[ *  *  * ]
Sorry for bumping this back...

None of these things are working for me. When ever I type in 5, it always says it's right, instead of picking a different number. Can someone give me the code I created, with the added part that will make it a random number each time?
Offline Profile Quote Post Goto Top
 
RyuraGS
Member Avatar
Member
[ *  *  * ]
What's your code look like right now?
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
Go to Next Page
« Previous Topic · jQuery Help · Next Topic »
Add Reply