473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to Use Random Numbers to Change Variables

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 2417
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
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
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
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
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
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
@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
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,...
10
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...
3
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...
10
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...
16
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...
104
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...
3
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...
8
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...
24
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 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.