How to Use Random Numbers to Change Variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dickwhyte
    New Member
    • Feb 2010
    • 7

    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:

    Code:
    <script language="JavaScript">
    var author="www.dseffects.com";
    var image="dot.jpg";
    var distance="1";
    var speedx="1";
    var speedy="1";
    var regkey="";
    var reglink="";
    var regtarget="";
    </script>
    <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
    Last edited by Dormilich; Mar 3 '10, 11:41 PM. Reason: changed title
  • dickwhyte
    New Member
    • Feb 2010
    • 7

    #2
    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

    Comment

    • dickwhyte
      New Member
      • Feb 2010
      • 7

      #3
      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.

      Comment

      • Lazarus
        New Member
        • Sep 2008
        • 5

        #4
        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

        Comment

        • dickwhyte
          New Member
          • Feb 2010
          • 7

          #5
          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

          Comment

          • dickwhyte
            New Member
            • Feb 2010
            • 7

            #6
            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

            Comment

            • dickwhyte
              New Member
              • Feb 2010
              • 7

              #7
              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).

              Comment

              • Lazarus
                New Member
                • Sep 2008
                • 5

                #8
                Originally posted by 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??

                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
                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

                Comment

                Working...