PDA

View Full Version : how Random text that does not repeat



falisco
06-30-2016, 11:53 AM
I want to have random (texts, unique codes) that appear upon refreshing the page.
The idea is I want to send people to a page where they can get access to a code like this for example: 9KKR-GMMGWX-WXEJYL
and when they refresh the page, or a new person visits the page a new code would appear like this for example: 55E9-V4SHSR-7A6JCL


However, what I want to do is have it so that it does NOT repeat a code if they have previously been selected.

I found this javascript in this website which is close to what I'm trying to accomplish:


<html>
<head>
<script type="text/javascript">

var pic = [];
pic[0] = "Adam Arnold";
pic[1] = "Brenda Beeston";
pic[2] = "Carl Carlton";
pic[3] = "Deborah Daybrook";
pic[4] = "Eric Eastwood";
pic[5] = "Francis Findern";
pic[6] = "Greg Grantham";
pic[7] = "Hilda Heanor";
pic[8] = "Ian Ilkeston";
pic[9] = "Jenny Japan";
pic[10] = "Keith Kimberley";
pic[11] = "Lucy London";

function pupilsname() {

randomNum = Math.floor(Math.random()*pic.length);
return pic.splice(randomNum, 1)[0] || "Sorry, no more names left.";
}

function tools() {

var pic = new Array();
pic[0] = "Pick a name from the hat";


randomNum = Math.floor(Math.random()*pic.length);

return pic[randomNum];
}


</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body><center>
<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top" bgcolor="#FFFFFFF"><table width="80%" height="220" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="60" colspan="2" align="center" valign="middle" bgcolor="#FFFFFF"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="middle"><font size="6" color="#000000"><strong>Big Wood School Random Name Generator</strong></font></td>
</tr>
</table></td>
</tr>
<tr>
<td width="50%" rowspan="2" align="center" valign="middle" bgcolor="#FFFFFF"><p> <a href="#"
onMouseOver="document.getElementById('myText1').innerHTML = pupilsname() ;" onMouseOut="document.getElementById('myText1').innerHTML = tools() ;""><font size="3"><font size=5><strong>Pick a pupil</strong></font></a>
</li>
</p></td>
<td width="50%" bgcolor="#FFFFFF">&nbsp;</td>
</tr>
<tr>
<td align="left" valign="top" bgcolor="#FFFFFF"><font size="6" color="#000000">
<div id="myText1" onMouseOver="this.innerHTML = tools();">Pick a name from the hat</div>
</font></td>
</tr>
</table></td>
</tr>
</table></center>
</body>
</html>

However instead of names being changed on mouse over, I want unique codes to change upon refreshing the page, no table needed nor headline needed
I just want the codes to show up randomly without repeat, and if all the codes showed up, a message would appear saying " sorry no more codes left"


I know this is possible,

your help is much appreciated

thanks


Many thanks,

jscheuer1
06-30-2016, 04:07 PM
You can't do it like that using javascript. Each time the page is refreshed/navigated to, javascript forgets what happened before, so there can be no progression. There could and would sometimes be repeats. But you can do other things that will result in a unique code that is also somewhat random (pick at random from a list - append a timestamp to make it unique). Or you could take your chances and randomize each character out of the alphabet and 1 to 9, maybe throw in dashes and underscores, randomly choose upper or lower case for each character, choose 10 or so characters each time, and be reasonably sure it won't repeat (though it just might).

But it could depend on what you're really trying to do.

Is any random code OK? Or does it have to be one chosen from a predefined list? Do you need it to run out of codes (have a finite number)? If so, just for a single visitor or also for cumulative visitors?

Somethings like these can, as I say be done with javascript. But if you want to choose from a list, and have that list run out when the last code has been given, and have that happen across users, you would need a server side script (one written in PHP or asp or other server side code). Javascript couldn't do that.

So, what exactly is this for?

falisco
06-30-2016, 04:29 PM
hi jscheuer1, thx for your quick reply,


the codes have to be chosen from a predefined list
if the codes run out, a message would appear" sorry no more codes left"

cumulative visitors.

the javascript code is actually yours I found it in this website and it worked,

I just want the codes to change instead of the names and upon refresh

I found this code in another website, it worked but it repeats:


<span id="randomText"></span>

<script>

// self instancing fucntion to isolate scope
(function(d) {

// handy helper functions first

function cookieGet(name) {
var reg = new RegExp('(?:^|; )' + encodeURIComponent(name) + '=([^;]*)').exec(d.cookie);
return reg ? reg[1] : null;
}

// use the DOM instead of innerHTML to avoid a reparse

function nodeAdd(e, newNode) {
e.appendChild(
typeof newNode == 'object' ? newNode : d.createTextNode(newNode)
);
}

function nodeFlush(e) {
while (e.firstChild) e.removeChild(e.firstChild);
}

function nodeReplace(e, newNode) {
nodeFlush(e);
nodeAdd(e, newNode);
}

// then our actual functionality

var
textList = [
"All the leaves are brown,",
"and the sky is grey.",
"I've been for a walk,",
"on a winter's day.",
"I'd be safe and warm,",
"If I was in L.A.",
"California dreaming,",
"On such a winter's day."
],
expires = 'Sat, 6 Jun 3057 22:32:00 -04:00', // RIP Myndo Waterly
current = cookieGet('currentRandomText');

current =
current == null ?
Math.floor(Math.random() * textList.length) :
Number(current);

nodeReplace(d.getElementById('randomText'), textList[current]);

d.cookie = 'currentRandomText=' + (
(current + 1) % textList.length
) + '; expires=' + expires;

})(document);

</script>

how can you fix it so the first sentence don't repeat and instead a "sorry, no more codes available" show up

thanks

jscheuer1
07-01-2016, 01:59 AM
Setting a cookie was something I thought of. But a cookie set by one user cannot be read/utilized by/influence another user. So, if I understand you correctly (cumulative across multiple users), you will need server side code for this to work as intended. What's supposed to happen when all codes are used? The next, and all subsequent person(s) get a 'sorry' message? Do they (the list of codes) ever reset?

Anyways, this is pretty simple to set up server side. PHP is the server side language I'm most familiar with and the one usually most available from a given host.

If I have misunderstood, and the limitation on codes applies only to a given user (other users could come along and get the same code as some other previous user), then cookies could work - sort of. Bear in mind that cookies can be deleted and/or not be enabled. So even with this approach, server side coding would be preferable, especially if the constraints you want to impose are important.

falisco
07-01-2016, 01:13 PM
the idea is to give one code per visitor
so if another visitor goes to the page a new code would show up.
and if all the codes, let's say for example 100 showed up
visitor number 101 would get a messages saying" sorry no more codes are left"

without repeating code number 1.

I used the code above, what I liked about it is that the codes are in order not random,
so I just added 100 " sorry no more codes are left" at the end after adding 100 "codes"
to make sure code number 1 won't repeat, at least after another 100 visits

I think this is a temporary fix, until you can come up with something more clever :)

thanks

jscheuer1
07-01-2016, 03:22 PM
But you're the only one testing. If someone else goes to a page like that, they could get the same code. The cookie is only on your computer. Only for your browser even, so you could see the problem even just by using a different browser from the same computer, or even by just clearing your cookies.

To get what you want requires server side code. Do you have PHP available?

falisco
07-01-2016, 04:21 PM
could you please write a php code?
I'll try to add it to my page
Thanks

jscheuer1
07-02-2016, 01:06 AM
It won't do any good if the page that it's intended to be used upon cannot run PHP.

Are you saying you don't know if your host allows/furnishes PHP?

In any case, even if they do, there are certain conventions that must be observed, you cannot just add a script to a page like one can when using javascript.

And, let's first make sure you can run PHP on your host. Make up a page:


<?php
phpinfo();
?>

Save that as - say, php_info.php

Upload it to the root folder of your host and navigate to it. If PHP is active, you should see a header similar to (click for larger view):

5910

followed by a lot of information. If not, it's (PHP) is not currently available.