Jump to content

Random Prize Generator


ChidoriSoul

Recommended Posts

Ok, so i have a forum, is it possible to have a code that where each page you naviagte, you find a random thingLike there is a 1/1000 chance to find a dog, or somethingEvery page you navigate, you try again, and againthen when you are lucky, you win, and it tells you, you winlike, you are on the index and it saysYou have Lost, sorry, try againthen you referesh, and it saysYou have Won!

Link to comment
Share on other sites

Use mt_rand to generate a random number. If you want a .1% chance, then generate a number between 1 and 1000 and check if the number is 1.http://www.php.net/manual/en/function.mt-rand.phpI have the exact same thing in one of my applications.

// do housekeeping every X requests on average$rand = mt_rand(1, 1000);if ($rand == 1){  //error_log("do housekeeping");  $db->sql('DELETE FROM cache WHERE timest <= %d');  $db->add_param(time() - (60 * $config['cache_timeout']), false);  $db->delete();  $db->sql('OPTIMIZE TABLE `cache`');  $db->exec();}

Whoops, Javascript, right.Use math.random instead:https://developer.mozilla.org/En/Core_JavaS...cts:Math:randomSome examples on that page.

Link to comment
Share on other sites

Yeah, math.random is what you use to get a random number. Once you have one or two or however many random numbers you want, you can do whatever else you want your application to do. But math.random is the way to generate the random number in the first place.

Link to comment
Share on other sites

lol learn javascript?

function check() { input = document.getElementById("Your text field or whatever id").value; prizeWinningNumber = Math.round(Math.random() *1000); if (input == prizeWinningNumber) {	// Put you're code for the winning screen here }}

It's as easy as that?

Link to comment
Share on other sites

Is this what your looking for?

function GetNumbers() {	var Chance = Math.round(Math.random()*1000);	var Winner = 1;	var Output = "Nope sorry you lost :(";		if(Chance == Winner) {		Output  = "OMG It's so amazing you won!!!! :D";	}		ShowWinner(Output);}function ShowWinner(Output) {	alert(Output);}window.onload = GetNumbers;

Link to comment
Share on other sites

Ok, but how can I modify it to display the info, not make a popup box?
function GetNumbers() {	var Chance = Math.round(Math.random()*1000);	var Winner = 1;	var Output = "Nope sorry you lost :(";		if(Chance == Winner) {		Output  = "OMG It's so amazing you won!!!! :D";	}		ShowWinner(Output);}function ShowWinner(Output) {	window.location = "WinningPage.html";}window.onload = GetNumbers;

Something like that I think what ever is in the ShowWinner function will be displayed if the numbers match.

Link to comment
Share on other sites

ChidoriSoul, two people have given you a method for generating your random number. But I think no one understands what you want to do with it. Please explain your goal more clearly.

Link to comment
Share on other sites

Ok, I have a forum and I want to get it so each main page you navigate there would say either of these two thingsYou did not win, please try againor, if the numbers match,You have won, visit this Linkwhere every page you visit the number generator switches, and if the numbers match, the winning text is shown, but if they don't, the losing text is show

Link to comment
Share on other sites

If you want to add some text to your document, you need a place to put it. So create an HTML element like this:<div id="output"></div>Now your script can print a message like this:message = "Hello";document.getElementById("output").innerHTML = message;Notice how the id of the div is the same id being passed to document.getElementById().

Link to comment
Share on other sites

Yes, I know, the first code that RenegadeFX gave me worked, and it was fine, I am just asking how to make it so that it appears as text, not an alert box saying if you have won or lost, and that is what I am asking for. Sorry, I am not a JS expert -.-

Link to comment
Share on other sites

Yes, I did, I am just unsure where to organize everything around.message = "Hello";document.getElementById("output").innerHTML = message;About this code, where does the actual code that I want to go in here, and what does "output" do?

Link to comment
Share on other sites

This line:document.getElementById("output").innerHTML = message;should replace the alert that RenegadeFX showed you in Post #6."output" is the id of the div where you want to display the message. It is not the same as the Output variable that RenegadeFX showed you. I'm sorry if using the same word confused you.I don't know where in your document you want the <div> to appear. For now, you can put it at the top just to make sure everything works. Then move it later.

Link to comment
Share on other sites

<script type="text/javascript>function GetNumbers() {	var Chance = Math.round(Math.random()*1000);	var Winner = 1;	var Output = "Nope sorry you lost :(";		if(Chance == Winner) {		Output  = "OMG It's so amazing you won!!!! :D";	}		ShowWinner(Output);}function ShowWinner(Output) {	document.getElementById(output).innerHTML = Hi;}window.onload = GetNumbers;</script>

Ok, so this is my entire code, yet it still shows nothing on the page? Is there an error

Link to comment
Share on other sites

Do you want me to change the part where it is like document.getElementById(output).innerHTML = Hi; to what you just wrote
Yes. You needed to put quotation marks around your id. I also thought we should put the correct text into your new div. (If you still want to print the word "Hi" it should also go in quotation marks.)
Link to comment
Share on other sites

I did what you told me to do, yet it still does not work, could you just point out what i did wrong. then let me see what it should be like?

<script type="text/javascript>function GetNumbers() {	var Chance = Math.round(Math.random()*1000);	var Winner = 1;	var Output = "Nope sorry you lost :(";		if(Chance == Winner) {		Output  = "OMG It's so amazing you won!!!! :D";	}		ShowWinner(Output);}function ShowWinner(Output) {	document.getElementById("output").innerHTML = Output;}window.onload = GetNumbers;</script>

Link to comment
Share on other sites

It's always the simple things. :) Your script will work just fine (I tested it) if you correct one simple error, which I should have noticed sooner. This line:<script type="text/javascript>requires a closing quotation mark:<script type="text/javascript">So just add that. If your <div> is correct, all should work.

Link to comment
Share on other sites

Wow, how do I miss the simplest things :)Well, I still does not work (This is the page I want it on)The overall code is

<script type="text/javascript">function GetNumbers() {	var Chance = Math.round(Math.random()*1000);	var Winner = 1;	var Output = "Nope sorry you lost :(";		if(Chance == Winner) {		Output  = "OMG It's so amazing you won!!!! :D";	}		ShowWinner(Output);}function ShowWinner(Output) {	document.getElementById("output").innerHTML = Output;}window.onload = GetNumbers;</script>

The page is just the code, there is no other words, or anything else

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...