Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

create random number Javascript based on mouse position

Posted on 2014-02-14
2
519 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Binding a Grid in Javascript 5 43
what is the best Integrated development environment 2 20
send email part1 9 27
IF statment In Powershell 12 15
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 …
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

856 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