# [...] rand funct is time based
Well ... it might be. The random function has to be initialized with something randomish like the time (but it can also be the position of the mouse, or how much it has moved since start up, the mean typing speed of the user in milliseconds when he entered his password, etc ...), but initialization is only done once. You can check rand's documentation, which tells that srand is called, unless it has already been done. So after your first rand, the time does not change anything. You are actually just wasting time there.
Do not call srand() multiple times in your program unless you know exactly what you're doing and why you're doing it. [...]. Just do it once at the top of your program, or you won't get random numbers out of rand()!
This means that you should probably trust rand to be random enough for you, and not try to affect its behavior by messing with it. srand's documentation advises you to have a look at Math::TrulyRandom if you think rand does not do its work properly.
Using rand until you have a number that fits your expectation isn't a very good idea either, that's wasting time again. If you want to make sure you have a number that's n digit long, without leading 0, you could just do :
my $n = 10;
my $lastDigits = int rand(10**($n-1));
my $firstDigit = 1+int(rand(9));
my $number = $firstDigit.$lastDigits;
print $number;
| [reply] [Watch: Dir/Any] [d/l] |
| [reply] [Watch: Dir/Any] [d/l] [select] |
What made you think the rand function is time based?
Try adding srand(1000) to the start of your code and see what happens. You're just wasting time with those usleeps.
Designing a good random number generator isn't easy, so it's better to use one of the RNG modules that has been properly tested. see rand for some suggestions.
The maths behind random number generators and their proper testing is really interesting and well worth some study, if you have the time.
| [reply] [Watch: Dir/Any] [d/l] |
How do you like my RNG?
Not much.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [Watch: Dir/Any] |
Donald Knuth's celebrated masterwork, "The Art of Computer programming", vol. 2 (semi-numerical algorithms), has 193 pages on random numbers in my edition (3rd). You might guess from that that this is a pretty complicated and tricky subject. I would submit that you don't try to do better than existing packages before you've read and understood most of this material (or equivalent from other really authoritative sources).
| [reply] [Watch: Dir/Any] |
I original set out to just make an RNG to fit a certain length of chars but i though i would try to make it a bit more random. Thanks for the suggestions.
| [reply] [Watch: Dir/Any] |
yeah. I didn't think it was a simple subject; I just Thought I'd have so fun with the rand function. It's not for a business or anything.
Also I'm playing around with my next iteration of this (which is basically non existent anymore sense so much of my old code is flawed).
| [reply] [Watch: Dir/Any] |