Issue with Random
BlitzPlus Forums/BlitzPlus Beginners Area/Issue with Random
| ||
Hello. In this code I am trying to make random strings each time. But the output that comes out in some of the strings is being repeated for some reason. The only thing that continually changes each time is the record number. I tried using RAND before using RND and I get the same results. What am I doing wrong? Thanks For II=1 To PRL:SeedRnd MilliSecs():RC$="":NA1$="":NA2$="":AG1$="":PH$="":AD$="":AD1$="":CI$="":ST$="":ZI$="":CM$="":FR$="" RC$=String$("0",7-Len(Str$(II)))+Str$(II):;Print RC:HALT For G=1 To 20:NA1$=NA1$+Chr$(64+Rand(1,26)):Next For G=1 To 20:NA2$=NA2$+Chr$(64+Rand(1,26)):Next AG1$=Str$(Rand(18,99)):;Print AG1: PH$=Str$(Rand(2000000000,9999999999)):;Print PH: AD$=Str$(Rand(10000,99900)):For G=1 To 30:AD1$=AD1$+Chr$(64+Rand(1,26)):Next:;Print AD$+" "+AD1 For G=1 To 20:CI$=CI$+Chr$(64+Rand(1,26)):Next:;Print CI For G=1 To 20:ST$=ST$+Chr$(64+Rand(1,26)):Next:;Print ST ZI$=Str$(Rand(10000,99999)):;Print ZI$: For G=1 To 256:CM$=CM$+Chr$(64+Rand(1,26)):Next:;Print CM$:HALT FR$=RC$+"|"+NA1$+"|"+NA2$+"|"+AG1$+"|"+PH$+"|"+AD$+" "+AD1$+"|"+CI$+"|"+ST$+"|"+ZI$+"|"+CM$ |
| ||
you should put the SeedRnd (Millisecs) *outside* of your loop -- just call it ONCE, at the start of your program. If your loop takes less than 1ms to complete (or even a bit more than that, if your computer happens to have a lower resolution timer) then the second time the loop runs, millisecs() may still be same. If you call SeedRnd with the same start value, the results will repeat as well. |
| ||
The ability to produce the same sequence of random numbers with a generator is a very useful thing. There may come a time when you actually want to have control over the seed (either the default generator one you write yourself). Depending on the purpose of the generator it can either be extremely simple to write a suitable generator or next to impossible (without special hardware). Example...writing a generator to create a list of semi random rpg character names... not too hard. Writing a generator for a gambling device a lot harder. |
| ||
From what I can gather from reading your posts is that I need to make the machine wait until the next machine cycle. Since the cycle would be a different length on different machines, what method should I use to find that length? |
| ||
From what I can gather from reading your posts is that I need to make the machine wait until the next machine cycle. No, just do what xlsior said - put SeedRnd(Millisecs()) at the start of your code. You only need to call it once (not once per loop - just once, ever). |
| ||
OK I see. I thought it would make the outcome more random. It does not. Its running as random as it can be. Thanks |