Solved

create random number Javascript based on mouse position

Posted on 2014-02-14
2
507 Views
Last Modified: 2014-03-11
Hi

want to create a random number between 1 and 0.5 based on mouse X position.

if the x posiion is over to left of screen i want the number to decrease and increase if mouse dragged to the right

How can i do this with javascript/jquery

Thanks
0
Comment
Question by:coolispaul
2 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39858913
Not sure I follow. Maybe explain in more detail what you need.

Do you want to generate a random number between 0.5 and 1 and then decrease if the mouse is over the left and increase it if it's over the right - what do you want to increase and decrease it to.

How do you want to track the X position of the mouse - click, move etc.

This will generate a random number between 0.5 and 1 - rounded to 2 places:

myNumb = (parseFloat(Math.random() / 2) + parseFloat(0.5)).toFixed(2);

Open in new window

0
 
LVL 9

Accepted Solution

by:
Sar1973 earned 500 total points
ID: 39864699
To create a number between 0.5 and 1:
var myRND=0.5*(Math.random()-1);
But if you want to convert the mouse X position to a range between 0.5 and 1 (and then not to get a random number):
var myXSIZE=0.5+event.clientX/(480*2); //IF YOUR SCREEN IS 640X480 IN RESOLUTION
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now