473,804 Members | 3,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Use Random Numbers to Change Variables

7 New Member
What I am trying to do is this: I have a program which makes Javascript SinusPaths. To make the SinusPath there are three values. What I wanted to know how to do was use a random number generator to assign the values everytime someone loaded the page so it would always come up different. Does that make sense? Is that something that Javascript can do?

I know basically how to generate the random number. I just don't know how to get that to be the value of something. Here is the code that I want to put the random numbers into:

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2. var author="www.dseffects.com";
  3. var image="dot.jpg";
  4. var distance="1";
  5. var speedx="1";
  6. var speedy="1";
  7. var regkey="";
  8. var reglink="";
  9. var regtarget="";
  10. </script>
  11. <script src="DS_SinusPath.js"></script>
And I want to add them to the var distance, the var speedx and the var speedy (which are presently set to 1).

Many thanks in advance to anyone who might be able to help me. Also - I would want each variable to have a DIFFERENT randomly generated number. Does this make a whole lot more complex??

All the best and thanks in advance,
Dick Whyte
Feb 27 '10 #1
7 2435
dickwhyte
7 New Member
Also - the reason I am asking this queston is for an art project I am working on. If you help with the solution you would get full credit on the final work if that is something you would like (I would like to give it to you anyway).

All the best,
Dick Whyte
Feb 27 '10 #2
dickwhyte
7 New Member
One solution I did think of was making pages for each combination of variables and then just using a random page loader to create the effect. But it would be a lot more work than a random number inserted into the variable.
Feb 27 '10 #3
Lazarus
5 New Member
I think you want something like this...

var distance = "" + Math.floor(Math .random() * 100 - 50);
var speedx = "" + Math.floor(Math .random() * 100 - 50);
var speedy = "" + Math.floor(Math .random() * 100 - 50);

Math.random() returns a number in range 0.0 to 1.0. The *100-50 makes the range -50.0 to 50.0. Math.floor() converts that to an integer and the ""+ sorta makes the final value a string, if thats necessary. Each call to Math.random() produces a new number.

Where you gonna post your project?
-w
Mar 3 '10 #4
dickwhyte
7 New Member
Thanks so much for that - it works a charm. The only thing I am now confused about is how to get it to give me the right range of values. I got a little lost in the description of how the number is made. I get that the initial number generated is between 0 and 1 (meaning 11 possibles - 0, 0.1, 0.2 and so on, up to 1). But then I wasn't sure what the *100-50 did - what is the final range of numbers??

What I am looking to have is a range from 1 to 10 or 11, tops. Anything faster than that and it kinda freaks out. And yeah, I wasn't quite sure what to do with the *100-50 in order to produce the results of 1-10 (also - the result cannot be 0).

Thanks again for your help - it clearly worked in terms of selecting a random value, but the value was just WAY too fast.

I figured out that the first value just determines the size, but not the shape, so I am leaving that as one for all of them. So the random value will be for the X and Y speeds. I will be posting the project on my own website at first. I have made a version that cycles through them in order (ie. 1-1, 1-2, 1-3, 1-4, 1-5, 2-1, 2-2, 2-3 and so on) but I am keen to do a random one as well. Once I am sorted with the number values I will post a version and put a link here for you to have a look if you want, and I would like to get some details in terms of how I should credit you. Some of my art can be seen here: http://www.wayfarergallery.net/hot-images which is a journal I am a part of, and also I write poetry and edit a haiku journal here: http://www.wayfarergallery.net/haikunews

You can find my email on haiku news under submissions, so if you wanted to email me your details for being credited for your work that would work. Or whatever suits you.

Many many thanks for your response, once again. That is SO awesome of you to help me out. I am quite keen to make a few javascript artoworks and I would be keen to collaborate with actual programmers as I start to figure out the kinds of things I am interested in.

All the best,
Dick Whyte
Mar 7 '10 #5
dickwhyte
7 New Member
Also - I don't know if you could be bothered, but I have been trying to get a menu to work with another script and am having some trouble. I posted the question here: http://bytes.com/topic/java/answers/...-what-can-i-do and would appreciate any help. Of course, if you don't have the time or inclination I do not expect anything from you. Thanks for helping out in general!!

All the best,
Dick Whyte
Mar 7 '10 #6
dickwhyte
7 New Member
Actually, I uploaded the version which just cycles through them (with the variables 1-5). You can see it here: http://www.wayfarergallery.net/netwo...ter/1-1-1.html It will cycle through them naturally. When viewing it on my computer it all matches up as well, but it seems to go out of synch on the online one, so I will have to work on the delay times (because the end of the run is meant to meet up with the beginning of the next run).
Mar 7 '10 #7
Lazarus
5 New Member
@dickwhyte
Thanks so much for that - it works a charm. The only thing I am now confused about is how to get it to give me the right range of values. I got a little lost in the description of how the number is made. I get that the initial number generated is between 0 and 1 (meaning 11 possibles - 0, 0.1, 0.2 and so on, up to 1). But then I wasn't sure what the *100-50 did - what is the final range of numbers??
Math.random() returns a floating point (decimal) number in range 0.0 to 1.0 meaning you'll get stuff like 0.3482182. From that you need to do a little math to get the final values you want. Basically you multiply it to scale the range then add a number to shift it's starting range. A random number in range [0.0 - 1.0] when multiplied by 7 will then be in range [0.0 - 7.0]. Add 4 to that to get a number in range [4.0 - 11.0].

Um, I've been a little misleading... Math.random actually won't return 1.0. It's range is more like [0.0 - 0.999999].

What I am looking to have is a range from 1 to 10 or 11, tops. Anything faster than that and it kinda freaks out. And yeah, I wasn't quite sure what to do with the *100-50 in order to produce the results of 1-10 (also - the result cannot be 0).
This function will return a number for a given range. (untested

function integerInRange( lowerBound, upperBound) {
return Math.floor( Math.random() * (upperBound-lowerBound+1) ) + lowerBound;
}

For your needs use it like...
var z = integerInRange( 1, 10);

Thanks again for your help - it clearly worked in terms of selecting a random value, but the value was just WAY too fast.
I figured out that the first value just determines the size, but not the shape, so I am leaving that as one for all of them. So the random value will be for the X and Y speeds. I will be posting the project on my own website at first. I have made a version that cycles through them in order (ie. 1-1, 1-2, 1-3, 1-4, 1-5, 2-1, 2-2, 2-3 and so on) but I am keen to do a random one as well. Once I am sorted with the number values I will post a version and put a link here for you to have a look if you want, and I would like to get some details in terms of how I should credit you. Some of my art can be seen here: http://www.wayfarergallery.net/hot-images which is a journal I am a part of, and also I write poetry and edit a haiku journal here: http://www.wayfarergallery.net/haikunews
You can find my email on haiku news under submissions, so if you wanted to email me your details for being credited for your work that would work. Or whatever suits you.
Thanks but I don't need to be credited, but I like to see peoples works :)

------------------------------------------------------
Also - I don't know if you could be bothered, but I have been trying to get a menu to work with another script and am having some trouble. I posted the question here: http://bytes.com/topic/java/answers/...-what-can-i-do and would appreciate any help. Of course, if you don't have the time or inclination I do not expect anything from you. Thanks for helping out in general!!
I see you got a response to this, hope it works out, I don't know.

-----------------------------------------------------
Actually, I uploaded the version which just cycles through them (with the variables 1-5). You can see it here: http://www.wayfarergallery.net/netwo...ter/1-1-1.html It will cycle through them naturally. When viewing it on my computer it all matches up as well, but it seems to go out of synch on the online one, so I will have to work on the delay times (because the end of the run is meant to meet up with the beginning of the next run).
Looks like your using a 3rd party script to animate and reloading a page to get the next set of vars for it. This can be done in a single page but it depends on how SinusPath.js works. To have it sync perfectly you'd need to know where it is in the animation but since SinusPath is encrypted/obfuscated I don't think it's practical to get at that. -w
Mar 10 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3803
by: Ante Perkovic | last post by:
Hi, everybody First, the code: --------------------- $maxNo = 500 $numbers = range (1,$maxNo); srand ((double)microtime()*1000000); shuffle ($numbers); $subarray = array_slice ($numbers, 0, 24); ksort ($subarray); // this also don't work!!!
10
11963
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was using to get random numbers was Random rn = new Random(System.currentTimeMillis()); but it seems that the system doesn't update the milliseconds often enough to cause a true randomaziation (i ran just the System.currentTimeMillis() in a
3
2688
by: Heath | last post by:
Using MSIE 5+ Heath writes: My problem deals with working with window objects between pages as follows: My Introduction page contains a link to my Action page. The onClick of that link creates a series of random numbers that are appended to the end of the url of the Action page. After initiating the link the URL of the Action page looks like this:
10
2511
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone can help it would be greatly appreaciated, I have tried many of
16
4230
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have shelled out on VC++ 6.0 compiling that same code is proving difficult. I am not too worried how I generate random numbers in c++, as long as it is sufficiently random. Can anybody help me out in getting what I want from VC++?
104
5200
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random prime.
3
24552
by: JoelPJustice | last post by:
I am working through a VBA book by myself to help and try and improve my skills. However, the book does not give you solutions to certain problems. I have worked through this problem up until bullet point 3. Here is the code I came up with up until now. Function RandomNormal(Mean As Double, StdDev As Double) As Double Application.Volatile Randomize RandomNormal = (StdDev * Sqr(-2 * Log(Rnd)) * Cos(2 * 3.141596 * Rnd)) + Mean End...
8
3918
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new Random((int)_seedTimer.ElapsedTicks); _random = new Random(randomSeed.Next(0,99999999)); return random.Next(min, max);
24
7238
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me uniformly distributed and unique numbers ?
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10327
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.