Skip to main content
Participating Frequently
May 27, 2008
Question

RandRange returns the same number in a loop

  • May 27, 2008
  • 5 replies
  • 623 views
In this code, when going through the loop, randNum doesn't change to a different value (which is a bit of a problem and not very random if you ask me). Is there something I am doing wrong to produce the same random number through the loop?
    This topic has been closed for replies.

    5 replies

    Fernis
    Inspiring
    May 29, 2008
    We haven't yet seen where and how you display the random number. Can you show us how you know the number is the same every time?

    -Fernis
    Inspiring
    May 27, 2008
    > In this code, when going through the loop, randNum doesn't change to a
    > different value (which is a bit of a problem and not very random if you ask
    > me).

    You probably know this, but selecting "1,1,1" is just as likely (or, to put
    it another way, "random") as "591,7123,6996".

    However by the sounds of it, you are getting runs of the same number too
    frequently to consider this "random happenstance" (sic)?


    > Is there something I am doing wrong to produce the same random number
    > through the loop?

    Not that I can see.

    I've never had a problem with randRange() coming up with the goods
    vis-a-vis reasonably random results (random enough for any purpose I use it
    for, anyhow).

    As for Ian's thing about randomize(), I *suspect* that that applies to
    numbers generated by rand(). I was under the impression that randRange()
    dealt with the random seeding internally. Not that I've checked...

    --
    Adam
    Inspiring
    May 27, 2008
    tristanlee85 wrote:
    > I completely understand that computing a random number doesn't make it random,
    > but my issue was that in almost all cases, if I went through the loop say 3
    > times, the "random" number would be the same for all 3 cases. I'll give your
    > method a shot. Thanks.
    >


    Yeah, if you read the documentation on the Randomize() function you will
    see that this is specified behavior. For some types of
    development/testing purposes it is sometimes useful to be able to
    repeatedly generate the same series of random numbers.

    Participating Frequently
    May 27, 2008
    I completely understand that computing a random number doesn't make it random, but my issue was that in almost all cases, if I went through the loop say 3 times, the "random" number would be the same for all 3 cases. I'll give your method a shot. Thanks.
    Inspiring
    May 27, 2008
    tristanlee85 wrote:
    > Is there something I am doing wrong to produce the same random number
    > through the loop?

    Expecting a computer to create a truly random number. They are horrible
    at it. Really, really horrible.

    But to make the best of a bad situation, there is the randomize()
    function. If it is provided a constantly changing seed, then random
    numbers generated by the computer can appear random, to the average
    human at least.

    My preferred method is something like this.

    <cfloop index="thisProdID" list="#Attributes.ProductID#">
    <cfset randNum = "">
    <cfset randomize(timeFormat(now(),l)>
    <!--- this passes the current millisecond value to seed the
    following random number generation --->

    <cfset randNum = RandRange(1,9999)>
    <cfset lDetailID = ListAppend(lDetailID, thisProdID & "_" &
    TimeFormat(Now(), "hhmmss") & "_" & NumberFormat(randNum, "0000"))>
    </cfloop>