Home Entrance Everyone Wiki Search Login Register

Welcome, Guest. Please login or register. - Thinking of joining the forum??
April 27, 2024 - @367.38 (what is this?)
Forum activity rating: Four Star Posts: 79/1k.beats Unread Topics | Unread Replies | Own Posts | Own Topics | Random Topic | Recent Posts
News: :ozwomp: Reminder: Forum messages stay readable for years! Keep yours high quality! :ozwomp:

+  MelonLand Forum
|-+  World Wild Web
| |-+  ✁ ∙ Web Crafting
| | |-+  ☔︎ ∙ I need Help!
| | | |-+  HELP: How to make a random generator?


« previous next »
Pages: [1] Print
Author Topic: HELP: How to make a random generator?  (Read 390 times)
Corrupted Unicorn
Jr. Member ⚓︎
**



View Profile WWW

First 1000 Members!Joined 2023!
« on: July 08, 2023 @521.00 »

What I'm looking for is to be able to make a List of Things, to save and be able to edit said List of Things indefinitely, and then, upon clicking a button, it would randomly select one of the Things of the List and display it to me. Ideally, I'd like everything to be stored in-house (in-website?) but if there's no other way than to depend on external sites, so be it.

I've seen a few Neocitizens with a system like this for jokes, fun facts, or tips, but I can't recall them rn so I can't snoop into their websites to see how they did it  :drat:
Logged

shevek
Sr. Member ⚓︎
****


˚₊⁀꒷₊˚︰₊︶꒦꒷₊⊹︰꒷

iMood: daintyeco

View Profile WWW

First 1000 Members!Joined 2023!
« Reply #1 on: July 08, 2023 @546.96 »

Hi, I am on the go right now so sorry for the brief answer, but maybe something like my poetry page can help you? Feel free to look into the code :) it’s done with JavaScript though iirc (I can’t view page source right now)
Logged

Odo was just an idea. Shevek is the proof.
TheNothingMonster
Full Member ⚓︎
***


Hey psst! Wanna see something spooky?

StatusCafe: thenothingmonster

View Profile WWW

First 1000 Members!Joined 2023!
« Reply #2 on: July 08, 2023 @555.93 »

Here's an example of a simple random letter generator a bit different to shevek's for reference:

Code
<span id="text">A letter from a to d is going to generate here</span>
<br><button onclick="myFunction()">Generate!</button>

<script>
      function myFunction() {
      var r_text = new Array ();
      r_text[0] = "a";
      r_text[1] = "b";
      r_text[2] = "c";
      r_text[3] = "d";
      var i = Math.floor(4*Math.random())
      document.getElementById("text").innerHTML = r_text[i];
      }
</script>

To modify text, just replace the letters next to the r_text variables with the text you want to display.
To add more possible results, just add another row of r_text[var_number_here] = "string_here"; and change the number before *Math.random() according to the amount of r_text variables you have (keep in mind that 0 counts too, so if you have 6 variables for instance, the number below needs to be 7). I hope my descriptions make sense.

Here's another example with text and more possible results:


Code
<span id="text">Some text is going to generate here</span>
<br><button onclick="myFunction()">Generate!</button>

<script>
      function myFunction() {
      var r_text = new Array ();
      r_text[0] = "MelonLand is awesome!";
      r_text[1] = "I love ice cream.";
      r_text[2] = "*insert fun fact here*";
      r_text[3] = "Remember to drink water!";
      r_text[4] = "<p style='color:yellow;'>HTML and CSS works in such strings too. :]</p>";
      r_text[5] = "Am I text?";
      r_text[6] = "Bla, bla, bla...";
      var i = Math.floor(7*Math.random())
      document.getElementById("text").innerHTML = r_text[i];
      }
</script>

You can alter it as much as you wish to fit your needs. shevek's code works too!
If you do not understand something, ask. :grin:
Logged

~~~~~~~~~~~~
Corrupted Unicorn
Jr. Member ⚓︎
**



View Profile WWW

First 1000 Members!Joined 2023!
« Reply #3 on: July 08, 2023 @590.27 »

Hey, I just want to say, thank you for the quick & easy to understand response to you both!  :unite: Now I can make all the prompt generators I want, and a few more fun things. I'll be tinkering in the backstage, like always.

With that said, should there be any issues if say, I'd like to put two or more generators in the same page? I gave the second function a different name, of course, but still doesn't seem to work  :ohdear:
« Last Edit: July 08, 2023 @623.53 by Corrupted Unicorn » Logged

TheNothingMonster
Full Member ⚓︎
***


Hey psst! Wanna see something spooky?

StatusCafe: thenothingmonster

View Profile WWW

First 1000 Members!Joined 2023!
« Reply #4 on: July 08, 2023 @639.33 »

With that said, should there be any issues if say, I'd like to put two or more generators in the same page? I gave the second function a different name, of course, but still doesn't seem to work  :ohdear:

That's because you need to change all the variable names as well. In short, each function needs to be completely unique, along with its corresponding HTML tag ids and buttons.

Here's an example of two generators at once:


Code
<span id="text1">Pets will generate here.</span>
<br><button onclick="firstGenerator()">Generate!</button>

<span id="text2">Wild animals will generate here.</span>
<br><button onclick="secondGenerator()">Generate!</button>

<script>
      function firstGenerator() {
            var pets = new Array ();
            pets[0] = "Cats";
            pets[1] = "Dogs";
            pets[2] = "Goldfish";
            var i = Math.floor(3*Math.random())
            document.getElementById("text1").innerHTML = pets[i];
      }

      function secondGenerator() {
            var wild = new Array ();
            wild[0] = "Tigers";
            wild[1] = "Wolves";
            wild[2] = "Whales";
            var i = Math.floor(3*Math.random())
            document.getElementById("text2").innerHTML = wild[i];
      }
</script>

As you can see, the variables holding the strings in each function are different, one being called pets and the other wild. Similarly, the ids of the span tags in which the strings are generated are also different (named text1 and text2) and, of course, the buttons' onclick properties have their matching function.

With that in mind, you can add as many generators as you like, as long as they do not collide with each other in some way.
Logged

~~~~~~~~~~~~
Corrupted Unicorn
Jr. Member ⚓︎
**



View Profile WWW

First 1000 Members!Joined 2023!
« Reply #5 on: July 08, 2023 @653.07 »

Ohh, now that makes sense! Thank you very much!  :cheerR: It's good to have someone so understanding
Logged

Pages: [1] Print 
« previous next »
 

Vaguely similar topics! (3)

chiptune maker!

Started by cinniBoard © ∙ Music Room

Replies: 6
Views: 2059
Last post March 08, 2023 @308.04
by Gans
Earthbound Text Generator

Started by ObspogonBoard ♖ ∙ Video Games

Replies: 7
Views: 3852
Last post March 04, 2023 @848.91
by sig
An enemy from the game I'm trying to make

Started by Grafo VolaveruntBoard ➶ ∙ Art Gallery

Replies: 3
Views: 1413
Last post March 22, 2022 @61.42
by Grafo Volaverunt

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021, Simple Machines | Terms and Policies Forum Guide | Rules | RSS | WAP2


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi