PDA

View Full Version : Advice Wanted: Creating a Random Database


Leopard
02-08-2011, 05:12 PM
*

Problem solved; thank you to everyone for their quick and clever answers!

*

Okay so I'm making a Thing and it'll have a whole bunch of entries, then I want it so that I can basically just click something and it'll retrieve one of those entries at random.

I want this to be:

[list] Relatively simple
Private
Easy for me to access (i.e. not taking three hours to get a random thing chosen)
Easy to add more entries to

I know how to do this in Flash, but that wouldn't really be easy to add new entries to. I could also just number them and get a number from random.org, but that would be annoying.

I don't know anything about programming really, so no C++ etc.

I can use Flash, any Office app, anything online.

Any assistance is much appreciated.

OneMoreTime
02-08-2011, 05:17 PM
javascript would make things rather easy

there is a neat function VAR.push("text"); that lets you ad a new entry to an array quickly. then you can use a standard random number puller script.

You just need somewhere to host the script

nerdboy
02-08-2011, 05:22 PM
A couple of things to know.

Is this going to be published online?
Do you mind using database at all?

I'm thinking of creating a simple webpage when you can enter entries and randomly pick them out (using PHP).

Leopard
02-08-2011, 05:24 PM
In this instance it's going to be entirely private, so probably not online.

Database things are fine assuming it's something I can set up reasonably easily (i.e. if someone just gives me the basic example for 1-2 entries and I add however in manually myself.)

I'd need to install some sort of PHP server to use it on my own computer, wouldn't I?

I'm pretty good with software in general, just not a programmer or web developer, so while I can figure things out when I have something to start with, no good at building it from scratch.

focki
02-08-2011, 05:28 PM
The simpliest solution would be to do it in MS Excel. A bit harder, but still easier than configuring Apache or IIS would be MS Access. You can PM me for tips.

OneMoreTime
02-08-2011, 05:29 PM
I can hack together a normal html page with javascript plugged in. take all of 20 seconds lol.

Leopard
02-08-2011, 05:30 PM
I can do excel, though html would definitely be more appreciated, assuming it runs self contained (no extra servers etc.)

I can hack together a normal html page with javascript plugged in. take all of 20 seconds lol.

Would be much appreciated :)

nerdboy
02-08-2011, 05:33 PM
Simplest thing I can think of is, Microsoft Access, if you're running on Windows.

Or, just like OneMoreTime says, Javascript could be a good one too.. It can be run locally on your computer, without the need for hosting or database.

I can give give you a sample, but need to wait another 6 hours - after I finish work ;p

OneMoreTime
02-08-2011, 05:37 PM
thinking more, javascript cant actually write to a file. So entries would have to be entered manually in the the file, but they could be retrieved just fine.

wsedrf69
02-08-2011, 05:40 PM
This has been tested in a Google Docs spreadsheet, but may also work in Excel (though I promise nothing).


Enter your list items in the first column
Enter the length of the list in cell B2
Copy this formula into some cell: "=INDIRECT(ADDRESS(RANDBETWEEN(1,B2), 1))"
Your randomly selected list item will appear where the formula was entered
Profit?


You can refresh the random choice with Ctrl-R.

Leopard
02-08-2011, 05:43 PM
thinking more, javascript cant actually write to a file. So entries would have to be entered manually in the the file, but they could be retrieved just fine.

That'd be just fine. No problem manually entering everything, I just mean I can't do things like create a script from nothing myself.



Wise; Thanks a lot, will check that out. Though a html file would be ideal for me, I think.

OneMoreTime
02-08-2011, 05:45 PM
<script type="text/javascript">
/**
Simply add the following to the code to make either new truths or dares
dare.push("blah");
truth.push("blah");
**/
var dare = new Array();
dare.push("dare 1");
dare.push("dare 2");

var truth = new Array();
truth.push('truth 1');
truth.push('truth 2');


function rotate_dare() {
var i = Math.floor(Math.random()*dare.length);
document.getElementById('dare_content').innerHTML = dare[i];
document.getElementById('dice_roll').innerHTML = '';
}
function rotate_truth() {
var i= Math.floor(Math.random()*truth.length);
document.getElementById('dare_content').innerHTML = truth[i];
document.getElementById('dice_roll').innerHTML = '';

}

</script>
<table cellpadding="5" cellspacing="3" align="center" style="background-color:#EDE685">
<tr>
<td colspan="2" align="center" width="500" style="font-family:Times New Roman;"><b>Truth or Dare?</b></td>
</tr>
<tr>
<td align="center"><input type="button" value="Truth" border="0" onclick="javascript:rotate_truth()" /></td>
<td align="center"><input type="button" value="Dare" border="0" onclick="javascript:rotate_dare()" /></td>
</tr>
<tr>
<td id="dare_content" colspan="2" width="500" align="center">Go ahead, ask for a truth ... I dare you!</td>
</tr>
</table>

again, you would have to add new entries manually, but once they are added you can easily open up the HTML page in any browser and grab random dares/truths/whatever you want

Leopard
02-08-2011, 05:48 PM
<script type="text/javascript">
/**
Simply add the following to the code to make either new truths or dares
dare.push("blah");
truth.push("blah");
**/
var dare = new Array();
dare.push("dare 1");
dare.push("dare 2");

var truth = new Array();
truth.push('truth 1');
truth.push('truth 2');


function rotate_dare() {
var i = Math.floor(Math.random()*dare.length);
document.getElementById('dare_content').innerHTML = dare[i];
document.getElementById('dice_roll').innerHTML = '';
}
function rotate_truth() {
var i= Math.floor(Math.random()*truth.length);
document.getElementById('dare_content').innerHTML = truth[i];
document.getElementById('dice_roll').innerHTML = '';

}

</script>
<table cellpadding="5" cellspacing="3" align="center" style="background-color:#EDE685">
<tr>
<td colspan="2" align="center" width="500" style="font-family:Times New Roman;"><b>Truth or Dare?</b></td>
</tr>
<tr>
<td align="center"><input type="button" value="Truth" border="0" onclick="javascript:rotate_truth()" /></td>
<td align="center"><input type="button" value="Dare" border="0" onclick="javascript:rotate_dare()" /></td>
</tr>
<tr>
<td id="dare_content" colspan="2" width="500" align="center">Go ahead, ask for a truth ... I dare you!</td>
</tr>
</table>

again, you would have to add new entries manually, but once they are added you can easily open up the HTML page in any browser and grab random dares/truths/whatever you want

Thanks a lot, that's perfect :)

So I simply add dare.push('dare 3 is this') right? Are there any characters I can't use, " [ ] () etc?

OneMoreTime
02-08-2011, 05:52 PM
Thanks a lot, that's perfect :)

So I simply add dare.push('dare 3 is this') right? Are there any characters I can't use, " [ ] () etc?

ya, just continue the lists. the only problem would be if you try to use quotes. like dare.push('blah blah don't blah'); would mess things up cause it reads the quote in dont as the closing of the first one. so just avoid " and ' when possible i guess

EDIT: also just realized i used " for one group and ' for the other lol. best to use the same one for both

Leopard
02-08-2011, 05:53 PM
ya, just continue the lists. the only problem would be if you try to use quotes. like dare.push('blah blah don't blah'); would mess things up cause it reads the quote in dont as the closing of the first one. so just avoid " and ' when possible i guess

Okay, thank you very much, that's exactly what I wanted :)

nerdboy
02-08-2011, 06:06 PM
<script type="text/javascript">
/**
Simply add the following to the code to make either new truths or dares
dare.push("blah");
truth.push("blah");
**/
var dare = new Array();
dare.push("dare 1");
dare.push("dare 2");

var truth = new Array();
truth.push('truth 1');
truth.push('truth 2');


function rotate_dare() {
var i = Math.floor(Math.random()*dare.length);
document.getElementById('dare_content').innerHTML = dare[i];
document.getElementById('dice_roll').innerHTML = '';
}
function rotate_truth() {
var i= Math.floor(Math.random()*truth.length);
document.getElementById('dare_content').innerHTML = truth[i];
document.getElementById('dice_roll').innerHTML = '';

}

</script>
<table cellpadding="5" cellspacing="3" align="center" style="background-color:#EDE685">
<tr>
<td colspan="2" align="center" width="500" style="font-family:Times New Roman;"><b>Truth or Dare?</b></td>
</tr>
<tr>
<td align="center"><input type="button" value="Truth" border="0" onclick="javascript:rotate_truth()" /></td>
<td align="center"><input type="button" value="Dare" border="0" onclick="javascript:rotate_dare()" /></td>
</tr>
<tr>
<td id="dare_content" colspan="2" width="500" align="center">Go ahead, ask for a truth ... I dare you!</td>
</tr>
</table>

again, you would have to add new entries manually, but once they are added you can easily open up the HTML page in any browser and grab random dares/truths/whatever you want


Great code! Simple and get things done..

1 issue with the random algorithm though. Math.random() will generate value 1 > n > 0. So it will always be 0.12313 or whatever random decimal value it will find.

Thus:
var i = Math.floor(Math.random()*dare.length);

will always give you 0s until you have 'dare' entries more that 10.



The code should be:

var i = Math.floor(Math.random()*1000*dare.length) % dare.length;

That way, the random value will always be above 0, then get the remainder value when divided by number of 'dare' entries.

;)

Leopard
02-08-2011, 06:10 PM
I think I understand what you're saying there, but his code works perfectly for me with only two entries?

OneMoreTime
02-08-2011, 06:15 PM
normally, yes that is a concern, but with arrays you start with entry 0 not 1

of course, this has nothing to do with what you said, so ignore me :P

nerdboy
02-08-2011, 06:19 PM
I think I understand what you're saying there, but his code works perfectly for me with only two entries?

Actually his method works too. My bad. :p
floor(0.8 * 2) = 1
floor(0.4 * 2) = 0

Leopard
02-08-2011, 06:47 PM
Only semi related and entirely selfish question:

Is it possible to make it load a random image using javascript, too?

OneMoreTime
02-08-2011, 07:20 PM
ya, it would be a similar idea, assuming you have a handle on the HTML part to add the button in, you need something like

var image= new Array();
image.push("Image URL 1");


function rotate_image() {
var i= Math.floor(Math.random()*image.length);
document.getElementById('dare_content').innerHTML = "<img src='"+image[i]+"'>";

Of course the site color parsed it weird so its hard to see, but there is a nested quote in the innerHTML here, so a url with any sort of quotes will not work.

Leopard
02-08-2011, 07:22 PM
Thank you very much :)

Now to make it pretty, and my own little task generator will be complete :)

321tt
02-10-2011, 08:20 AM
As to image the image: if you have an image in the page, like
<img src="images/image0.jpg" id="myimg"/>

then in JavaScript you can put

x=5;
document.getElementById('myimg').src='images/image'+x+'.jpg';

and it'll do your job. Just don't forget the id property with the image.