Keeping track of Random plots
Blitz3D Forums/Blitz3D Programming/Keeping track of Random plots
| ||
What I am trying To do is have the computer randomly draw plots on the screen. The problem is I need to know the EXACT coordinates of EACH random plot drawn so I can use the data later in my program... how do I do this? ;;;;;;;;;;;;;;;;;;;;;;;;; SeedRnd (MilliSecs()) Graphics 800,600 For t = 1 To 200 Plot xmov,ymov xmov=xmov+0.6 ymov=ymov+Rnd(20,-15) Next WaitKey |
| ||
You don't need to store them - just store the random number seed.value = MilliSecs() 'draw some random plots seedRnd(value) Graphics 800,600 For t = 1 To 200 Plot xmov,ymov xmov=xmov+0.6 ymov=ymov+Rnd(20,-15) Next WaitKey ;draw the same 'random' plots again SeedRnd(value) For t = 1 To 200 Plot xmov,ymov xmov=xmov+0.6 ymov=ymov+Rnd(20,-15) Next |
| ||
if you want to store them you could use an Array:Dim VALUES(200) SeedRnd (MilliSecs()) Graphics 800,600 For t = 1 To 200 Plot xmov,ymov VALUES(t)=Rnd(20,-15) xmov=xmov+0.6 ymov=ymov+VALUES(t) Next WaitKey Cls WaitKey xmov = 0 ymov = 0 For t = 1 To 200 Plot xmov,ymov xmov=xmov+0.6 ymov=ymov+VALUES(t) Next WaitKey what gfk says is that rnd generates the same sequence of pseudorandomic values if you seed it with a specific seed value. Juan |
| ||
The sample of Gfk only works when you don't use Rnd somewhere else in the program. Eg: first draw 100 dots, then do something else (using Rnd), then draw the other 100 dots. It could mess up the order of the random numbers generated by Rnd. Unless you draw all dots in one go. The sample given by Charrua works better if you don't add new dots afterwards. When the array is re-dimensioned (when you add more dots), the contents are lost. In that case, you can use types: ; Setup the type-structure for a dot Type TDot Field X, Y ; Holds the X and Y coordinates of the dot End Type ; Seed the random number generator SeedRnd (MilliSecs()) ; Setup screen Graphics 800,600 ; Draw and store 200 dots For t = 1 To 200 ; Create a new TDot type-instance dot.TDot = New TDot ; Store the coordinates dot\X = xmov dot\Y = ymov ; Draw the dot on the screen Plot xmov,ymov ; Calculate the coordinates for the next dot xmov=xmov+0.6 ymov=ymov+Rnd(20,-15) Next ; Wait for a keypress WaitKey ; Clear the screen Cls ; Re-draw all dots (loop through each TDot type-instance For dot.TDot = Each TDot ; Draw the dot at the stored coordinates Plot dot\X, dot\Y Next ; Wait for a keypress WaitKey() ; End the program End |
| ||
Bear in mind that the seed may not generate the same random numbers on all computers. I think it may be processor related. It was discussed at length on several occasions a while back if you search the forums. |
| ||
While the generated random numbers may not be identical on different computers, at least the sequence itself should be identical if you re-run it on the same computer. |
| ||
The sample of Gfk only works when you don't use Rnd somewhere else in the program. Wrong. My code resets the random number seed when the number sequence needs to be repeated. |
| ||
Yes, I've used a similar method as Gfk suggests so that a more constant seed value is retained. By ensuring the Randomiser is seeded before each Rand or Rnd call, you can define whether the values will be predicted or truly (pseudo-)random. Note : The sue of Millisecs() for MySeed is only to ensure that the actual predicted sequence isdifferent each time the program is run, even though for that running of the program, provided MySeed doesn't change elsewhere, it will always follow the same sequence* Global MySeed%=MilliSecs() ;To use predicted values Print GetRandomInt(1,6,MySeed) ;To use typical pseudo random values Print GetRandomInt(1,6) Function GetRandomInt%(Low%=1,High%=2147483647,Seed%=False) If (Not(Seed)) SeedRnd MilliSecs() Else SeedRnd Seed% End If Return Int(Rand(Low%,High%)) End Function Function GetRandomFloat#(Low#=0.0,High#=2147483647.0,Seed%=False) If (Not(Seed)) SeedRnd MilliSecs() Else SeedRnd Seed% End If Return Rnd#(Low#,High#) End Function *For that particular cpu at least. |
| ||
Simple way:SeedRnd (MilliSecs()) Graphics 800,600 Const TotalPlots = 200 Dim Plots#(TotalPlots-1,1) Local xmov#,ymov# ;You forgot to declare your coordinates as floats. For t = 1 To TotalPlots Plot xmov,ymov Plots(t-1,0) = xmov Plots(t-1,1) = ymov xmov=xmov+0.6 ymov=ymov+Rnd(20,-15) Next WaitKey There you go. Now Plots(,) should have every plot made, and give you their coordinates. No types, no nothing (you can speed things up if you use two []-arrays). If you want the first plotted coordinates, read "Plots(0, xy)", xy being "0" for the X coordinate or "1" for the Y. If you want the last, read "Plots(TotalPlots-1, xy)". You can also use a FOR...NEXT loop to reproduce the plots in the exact order they were made, and seed-independent. Tip - the number inputted using DIM is actually the index number of last variable to be created within that particular index-range. So, if you do: Dim Array(200)You are actually creating a 201 element array. |
| ||
Wrong. My code resets the random number seed when the number sequence needs to be repeated. I've seen that you re-init the seed before the drawing, Gfk. This is true if you draw all 200 dots in one go, as I explained before. But if he splits up the drawing over multiple frames (or some other reason), and uses Rnd in between, then it could mess up the numbers. First draw 100 dots, then do something else (using Rnd to do something else), then go back to draw the remaining 100 dots, then the numbers aren't the same as you would draw all 200 dots in one go, without using Rnd in between for doing something else. value = MilliSecs() ;draw some random plots SeedRnd(value) Graphics 800,600 For t = 1 To 200 Plot xmov,ymov xmov=xmov+0.6 ymov=ymov+Rnd(20,-15) Next Print "Generating the dots for the first time" WaitKey Cls xmov = 0 : ymov = 0 ;draw the same 'random' plots again SeedRnd(value) For t = 1 To 200 Plot xmov,ymov xmov=xmov+0.6 ymov=ymov+Rnd(20,-15) Next Print "Re-drawing all dots in one go" WaitKey ; Draw the random dots again, using Rnd in between SeedRnd(value) Cls xmov = 0 : ymov = 0 For t = 1 To 100 Plot xmov,ymov xmov=xmov+0.6 ymov=ymov+Rnd(20,-15) Next ; Use another Rnd in between -> messes up the next 100 dots, as they won't be in the same position dummy = Rnd(20, -15) For t = 101 To 200 Plot xmov,ymov xmov=xmov+0.6 ymov=ymov+Rnd(20,-15) Next Print "Re-drawing all dots in 2 parts, using Rnd in between" WaitKey End |