Solved

create random number Javascript based on mouse position

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

706 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

19 Experts available now in Live!

Get 1:1 Help Now