Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
RAND() not very random
Message
General information
Forum:
Visual FoxPro
Category:
Other
Environment versions
Visual FoxPro:
VFP 7 SP1
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01155607
Message ID:
01155642
Views:
25
>Here's a funny one. No biggie but a bit annoying.
>
>I use the following code to generate a fairly unique filename for my exported XLS file, incorporating today's date + a random no. suffix, in case several XLSs are generated in one session/day, so they don't need to keep typing in a new name. This has been working fine till today. But today it keeps giving 851 and 856 (and others similar) off my m/c AND the 2 users' m/cs, such that my code, and Excel's interface keep asking if want overwrite.
>
>
>lcRandNum	  = ALLTRIM( STR( INT( RAND() * 1000)))
>lcDateStr	  = ALLTRIM( DTOC( DATE()))
>lcDateStr	  = STRTRAN( lcDateStr, "-", "_") + lcRandNum
>lcFilePath = PUTFILE("Export Results", "S" + lcDateStr, "XLS")
>
>
>Both when testing and today I've been issuing:
>
>? STR( INT( RAND() * 1000))
>
>repeatedly at the command line with expected varied results. Any ideas?
>
>'ppreciate it
>

Terry,

I ran a test and it seems that the randomness becomes more apparent when you 'zoom' the result of rand() with larger number. The test prg generates 1000 random numbers with the specified zoom level into a cursor "ccc", then extracts the unique numbers from "ccc" and creates a new cursor "ddd", by comparing the recccount() of the two cursors you're able to tell the generated numbers are unique or not. The prg is self-explanatory:
RandTest(1000) && Zoom level 1000 which is what you are using
RandTest(10000)
RandTest(100000)
RandTest(1000000)

FUNCTION RandTest
	LPARAMETERS lpnZoom as Integer 

	CLOSE TABLES ALL 

	*Make it as random as VFP gets
	RAND(-1)
	* Generate one thousand number
	CREATE CURSOR ccc (nr I)
	FOR i = 1 TO 1000
		INSERT INTO ccc VALUES (INT(RAND()*m.lpnZoom))
	NEXT 

	SELECT distinct nr FROM ccc INTO CURSOR ddd

	MESSAGEBOX("Zoom Level: " + TRANSFORM(m.lpnZoom,"999,999,999") + CHR(13) ;
		+ "Count of generated number: " + TRANSFORM(RECCOUNT("ccc"),"999,999,999") + CHR(13) ;
		+ "Count of distinct number: " + TRANSFORM(RECCOUNT("ddd"),"999,999,999"))

ENDFUNC && RandTest
As the test shows that if you are going to use rand()*1000, you're better off using a bigger number to multiply the result of rand() to increase the distinctiveness among the generated numbers.

I use the following to get a random my file name:
FUNCTION GetVFPUID(lpcPrefix as String)
	IF VARTYPE(m.lpcPrefix) = "C" AND ;
		NOT EMPTY(m.lpcPrefix) ;
	THEN 
		RETURN ALLTRIM(m.lpcPrefix) + Substr(SYS(3),5,4) + Substr(SYS(2015),7,4)
	ELSE 
		RETURN Substr(SYS(3),5,4) + Substr(SYS(2015),7,4)
	ENDIF 
ENDFUNC && GetVFPUID
HTH
Dawa Tsering


"Do not let any unwholesome talk come out of your mouths,
but only what is helpful for building others up according to their needs,
that it may benefit those who listen."

- Ephesians 4:29-30 NIV

Dare to Question -- Care to Answer

Time is like water in a sponge, as long as you are willing you can always squeeze some.

--Lu Xun, Father of Modern Chinese Literature

Previous
Next
Reply
Map
View

Click here to load this message in the networking platform