I want a gallery somehow similar like the link below.
https://securemg.lincoln.com/musicselfie/gallery
The codes are too complex. so I dont know how to do it.
Thanks
I don't know if you're a beginner, anyway, is not complicated create random image, for example:
<script language="JavaScript">
<!--
img = new Array()
ran = Math.floor(4 * Math.random());
img[0] = 'immagine1.jpg';
img[1] = 'immagine2.jpg';
img[2] = 'immagine3.gif';
img[3] = 'immagine4.jpg';
document.write("<img src=\""+img[ran]+"\">");
// -->
</script>
I've a data structure to store the web address of the image, has been used a vector, in which I included a link to each "cell" of the vector. Then, using "ran" (random number between 1 and 4) do the choice of the image to display.
The method Math.floor (number) displays the nearest whole to "number".
ES: Math.floor (12.5) becomes 12.
The random number is generated by switching to Math.floor with the Math.random() method (see Example Math.random).
To get a random number between 0 and 4 multiply the random number returned by math.random () for 4.
However you could play around CSS and other tricks to javascript in order to build not only a good code to show the images but also to create a truly personalized slide as the site. You could also take the CSS site and adapt them to your code ...
Well with this consideration the example should be clear.
Related
I am a java script beginner and I am trying to design a web page that shows two random numbers when refresh. According to these numbers a specific text should appear.
The numbers are images between 0 - 5.
as far, I wrote this code and it worked to change the images when refresh
var images1= new Array ("images/0.png","images/1.png","images/2.png","images/3.png",
"images/4.png","images/5.png");
var images2= new Array ("images/1.png","images/5.png","images/0.png","images/3.png",
"images/4.png","images/2.png");
function RandomImg() {
var x = Math.floor(Math.random() * images1.length);
var img1= document.getElementById('image1');
img1.src = images1[x];
var img2= document.getElementById('image2');
img2.src = images2[x];
}
RandomImg();
after that, according to the images appear, if the first number in the image is bigger than the second, a text should appear and and vice versa.
So my questions are "Is it possible to link each image to a number then execute if statement? or am i going in the wrong direction regarding this ? and how is possible to do that ?"
I did a lot of research but didn't reach anything clear since a week :(
Thanks :)
Yes, it is possible. You can use map in JavaScript to make a key value pair.
Just study about map and implement it.
https://developer.mozilla.org
I am trying to do a poor-mans version of multivariate testing on our website. What I want to do is use a piece of Javascript that will randomly choose between two URLS that I declare within the Javascript and use that as the source URL for the location.replace function.
I've not tried much because I am not a programmer in any sense of the word. If I have some code I can generally make my way through it, understand it and make small changes.
Here is the code that I am currently using:
location.replace("https://www.example.com/pageA.html");
What I want to happen is that when I fire the location.replace code it will will either choose
https://www.example.com/pageA.html
OR
https://www.example.com/pageB.html
I think I have to put all this in an array?
Any help would be most appreciated.
Here is an array based version.
You can have multiple amounts of URLs in the array and it will choose from them.
let urls = [
'https://www.example.com/pageA.html',
'https://www.example.com/pageB.html'
];
location.replace(Math.floor(Math.random() * urls.length - 1));
If you just want to randomly choose between two options, you could do something like this:
let rand = Math.floor(Math.random * 2)
if(rand){
location.replace("https://www.example.com/pageA.html");
}else{
location.replace("https://www.example.com/pageB.html");
}
Math.random will return a random float between 0 and 1, so multiplying by 2 and rounding down will give either 0 or 1.
0 will evaluate to false and 1 to true.
A disclaimer: I'm not familiar with Javascript. I've merely cobbled together a basic understanding of what I need to do for this task from Stack Overflow and other resources. My apologies if something below is unclear.
My problem: I need to generate a random number between 0 and 8,764, using Javascript, that will not repeat itself between Qualtrics survey responses.
Currently, I've found code to create an array that contains all numbers between 0 and 8,764, shuffles the array, and pops the last number off the end of the array.
It then adds embedded data to Qualtrics with that popped number, and I can then pipe the embedded data into a Qualtrics question to display it to my survey respondent. See below:
Qualtrics.SurveyEngine.addOnReady(function()
{
for (var i = 0, ar = []; i < 8; i++) {
ar[i] = i;
}
ar.sort(function () {
return Math.random() - 0.5;
});
var randomnumber = ar.pop();
Qualtrics.SurveyEngine.addEmbeddedData("randomnumber", randomnumber);
});
However, as far as I can tell, this Javascript code "resets" itself between survey responses, meaning it will re-create and re-shuffle the array each time a new respondent enters the survey. I'd like to find a way to make it so that it will be impossible for a new respondent to see the same popped "randomnumber" as a previous respondent. So, if the first survey respondent saw a 1, then the next survey respondent could see any number besides a 1 (let's say they see a 100 instead), and the next respondent could see any number except a 1 or a 100, etc etc.
I think it's possible to use embedded data in Javascript code and manipulate it (see here). It seems like there might be a way to access the randomnumber embedded data and write Javascript code to not remove any numbers from the array that match one of the previously popped randomnumbers. I lack the technical knowledge to execute this, if it's even the best way to accomplish the task.
Any and all help appreciated!
You can do what you want with Advanced Randomization in Qualtrics.
Set up a multiple choice question with your numbers 0 through 8,764 as the choices. Then use Advanced Randomization to select a random subset of 1 from all the numbers and click "Evenly Present" (Evenly Present is what tells Qualtrics to use every number before reusing any). Use JavaScript to hide the multiple choice question:
$(this.questionId).hide();
Now you can pipe your unique random number into a subsequent question. For example:
${q://QID1/ChoiceGroup/DisplayedChoices}
I'm tried to make some world generation mechanism using Math.random() whenever I needed something random, but then decided that I wanted it seed-based, so, given a seed, I changed all of the Math.random() to Math.sin(seed++)/2+0.5, hoping it would do the same thing, but would be the same if the seed was the same seed.
Then someone made me notice that the sin wave hasn't got even distribution, and finally I saw why some of my code was working strangely.
I was wondering if there was a simple fix, or if there isn't, another very simple seed based randomizer like this
So, I looked at your method, t1wc, and I found that it isn't actually evenly distributed. It is significantly more likely to spit out numbers near 0 or near 1 than it is to spit out numbers near 0.5, for example. This is just a consequence of the way that the sine function works.
Instead, you might try using a method called Blum Blum Shub (named after the authors of the original paper, wonderfully). It is evenly distributed and quite fast. Given a seed, it works as follows:
Square the seed and put the result in a temporary variable (x).
Take the mod of x base M.
M is a product of two large primes.
The value of x is a new seed to be used for future calculations.
Return x/M as your pseudo-random number. It will be evenly distributed between 0 and 1.
Below is a simple implementation of a Blum Blum Shub:
var SeededRand = function(seed, mod1, mod2)
{
return function()
{
seed = (seed*seed) % (mod1*mod2);
return seed/(mod1*mod2);
};
};
If you want to make a new random number generator, you just call:
var rand = SeededRand(seed, mod1, mod2);
Where seed is some initial seed (1234567890 works well), and mod1 and mod2 are some large primes (7247 and 7823 work well). rand is just a variable that I've defined to hold the output.
Now, to start getting random values, you just call:
rand();
Which will spit out a different value each time you run it.
If you have any questions, please ask!
There is a very nice seed-based randomizing script already made. It can be found here.
ok guys, found out this is what I'm really looking for:
(((Math.sin(seed.value++)/2+0.5)*10000)%100)/100
It sends out even spreaded numbers, and I guess it's a lot simpler than any other number generator I've seen
I have some code from someone but wondering why they might have used a function like this.
this.viewable= 45;
getGroups: function() {
return Math.ceil( this.getList().length / this.viewable );
}
Why would they divide the list length by a number viewable.
The result is the amount of items that should be rendered on the screen.
Why not just say 45 be the number. Is it meant to be a percentage of the list. Usually I will divide a large value by a smaller value to get the percentage.
Sorry if this seems like a stupid math question but my Math skills are crap :) And just trying to understand and learn some simple Math skills.
It's returning the number of groups (pages) that are required to display the list. The reason it's declared as a variable (vs. using the constant in the formula) is so that it can be modified easily in one place. And likely this is part of a plugin for which the view length can be modified from outside, so this declaration provides a handle to it, with 45 being the default.
That will give the number of pages required to view them all.
I would guess you can fit 45 items on a page and this is calculating the number of pages.
Or something similar to that?
This would return the total number of pages.
Total items = 100 (for example)
Viewable = 45
100 / 45 = 2.22222....
Math.ceil(2.2222) = 3
Therefore 3 pages
judging by the function name "getGroups", viewable is the capacity to show items (probably some interface list size).
By doing that division we know how many pages the data is to be divided (grouped) in order to be viewed on the interface. The ceil functions guarantees that we don't left out partial pages, if we had come records left that don't fill a complete page, we still want to show them and therefor make them count for a page.