random generation of number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mar11
    New Member
    • Sep 2009
    • 38

    random generation of number

    Hi all,

    I want to generate a number between 0 and 1023 and the difference between two generated number is always 2, e.g. 1, 3, 5, ...., 1023.

    through modulo I can generate a number between 0 and 1023 but I can not find a formula to generate the desired number sequence..

    any suggestion...

    Best wishes,
    Mar
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Are you saying you want to generate a number X between 0 and 1023 and then a second number that is either X+2 or X-2 or are you saying you want to generate an odd number in the range 1<= X <= 1023?

    Comment

    • mar11
      New Member
      • Sep 2009
      • 38

      #3
      I want to generate a number in the range between 0 and 1024. The sequence should be as the following: 0, 2, 4, 6,.. 1024. Each time I call the formula it should be generated one number like it is described above.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        If it is a sequence it is not random. Just start at 0 and add 2 every time.

        Comment

        • mar11
          New Member
          • Sep 2009
          • 38

          #5
          I meant by each call I want to get through random as following:

          1 call: generated 2
          2 call: generated 8
          3 call: generated 600
          4 call: generated 212

          and so on.. and the range is between 0 and 1024..

          I hope the idea is clear now..

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            So you want a number X such that 0 <= X <= 1024 and X is even.

            First question then is how many values are actually in that range? The answer 1024/2 + 1 = 513. So you need to generate 513 unique numbers or a number Y such that 0 <= Y < 513 or a number in the range 0 - 512.

            That should be easy using the modulus operator you mention in your first post. Then all you need to do is map those numbers onto the numbers you actually want, that is also easy

            X = Y * 2

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Generating a random integer between 0 and 1023 is straightforward . Is that what you want?

              I don't understand what you mean by "the difference between two generated numbers is always 2". The example you gave in reply #5 does not seem to obey that constraint.

              Is it important to you for your program to follow the same random sequence every time you run it; or is it important that the results be different each time you run the program? The answer to this question determines whether you need to seed the random number generator with a fixed constant or with a different value each time the program executes.

              Comment

              Working...