Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

RandRange returns the same number in a loop

New Here ,
May 27, 2008 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). Is there something I am doing wrong to produce the same random number through the loop?
597
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 27, 2008 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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 27, 2008 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 27, 2008 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 27, 2008 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 29, 2008 May 29, 2008
LATEST
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources