Random number?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Random number?

Post by raidho36 »

Can you post a .love that demonstrates this? I'd like to understand what you mean by patterns.
No. The program was written in C, and I have abandoned it along with the PC it was on. I didn't backed it up online because I thought the whole project (genetic transform of software that would control uniform robots to get optimal behavior) was too complex for me to handle and random program generator (for initial population, and random mutations) isn't much use elsewhere.

By patterns I mean sequences of same programs. Given estimated math probability of that two randomly generated programs would be the same is like probability of Earth collapsing into a black hole, I expected every program to be unique. But reality was that there were same programs, and they formed a pattern of such. Imagine if you represent a program with a color, and you draw those colors in a line, then there would be a pattern.
I was asking for a proper definition and would have accepted a number of answers (e.g. "high entropy", "uniformly distributed"). But not a vague feeling, come on.
Random has no math definition. It only has lexical definition, as a value cast outcome that you can't predict in any way.

Wikipedia reads "Commonly, it means lack of pattern or predictability in events."
All assumptions have been carefully considered and are valid within reason.
Reminds me of estimating terminal running velocity of a "shperical horse in a vacuum". It's widely known in russia, but you might not ever heard of it. It means that you can't apply these assumptions and simplifications, these don't work IRL.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Random number?

Post by Plu »

Sorry, but I'm still more likely to believe that you're either seeing patterns that aren't there or your program was just bugged. If your best argument is "I had proof but I deleted all of it" then you're not going to come across very convincingly... there's way too many ways in which your program could just be wrong.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Random number?

Post by raidho36 »

Well, I'm sorry for not having any solid proofs for you. You can stop believing me now. But it did happened to me, which is why I started to dig into random generators in the first place. And I don't think there was any random-related bug (what could be bugged in system default rand function?!), I ruled it out carefully. On a small array of programs it was giving unique instances, but once I raised plank to, like, 1 million programs, patterns have started to emerge.

(I just have edited it to million, since billion would be 1000 times million, and I was only uing 1000000. I'm not so good with english.)
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Random number?

Post by micha »

First of all, if the PRNG really had the problem that it produces repetitive patterns, then setting the seed would not solve this problem. PRNGs produce numbers from a long deterministic list. Setting the seed only changes the starting position within this list. So if there are repetitions in the list, setting the seed is not a solution.

Anyway I doubt that there are any recognizable repititions. Unfortunately you cannot give an example. I have two more explanations, that could explain why you saw repetitive patterns: First, it could be a bug. Second, it could be some some statistical effects, that occur when you combine many random numbers (as an example, what I mean by statistic: If you take the mean of a large number of uniformly distributed random numbers, this will converge to 0.5, even if the numbers are truly random).

You have to admit, that it is impossible to write a program that proves, that there are not patterns in the random numbers generated by math.random(). On the other hand, if there really were patterns, then it should be fairly simple to write a program that shows these patterns, right? I am still curious to see an example, where patterns become visible.

And last, the quality measure for randomness is not entropy. Entropy is a property of the underlying theoretical distribution and for the uniform distribution on {0, 1} it is log(0,5) which is approximately −0,6931. If a sample produced a different entropy, then this would not be a sign of better randomness, but a sign that the numbers a distributed according to a different distribution. If the random numbers are generated properly, then the sample entropy should be approximately the theoretical entropy.

The correct quality measure for randomness is not absolut, but depends on the application. If the random numbers, that I generate, work well for my purpose, then they are of good quality. This is the reason, why random numbers for scientific applications and for encryption are more difficult to generate than ones for games.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Random number?

Post by raidho36 »

PRNGs produce numbers from a long deterministic list. Setting the seed only changes the starting position within this list.
That's a common misknowledge, but no, actually. I have posted exactly the function used in every OS to generate pseudo-random number just a few posts ago.
it could be some some statistical effects, that occur when you combine many random numbers.
No. Random use was atomic rather than cumulative. In every iteration first random decide what function to execute, and subsequent random decide what arguments to use.
if there really were patterns, then it should be fairly simple to write a program that shows these patterns, right?
Yes, just about every lossless compression algorithm uses dictionaries.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Random number?

Post by vrld »

raidho36 wrote:
I was asking for a proper definition and would have accepted a number of answers (e.g. "high entropy", "uniformly distributed"). But not a vague feeling, come on.
Random has no math definition.
Probability theory and statistics beg to differ.
raidho36 wrote:
All assumptions have been carefully considered and are valid within reason.
Reminds me of estimating terminal running velocity of a "shperical horse in a vacuum". It's widely known in russia, but you might not ever heard of it.
I know of spherical cows, but I guess it's the same thing. However, this proverb applies to physics and engineering, not math and computer science - which we are talking about here.
You also still did not point out what assumptions are invalid. Unlike you, I provided an experiment you can verify and dissect.

Instead, you chose to claim to have run some sort of experiment that didn't work out because of poor quality PRNGs. You said you ruled out any bugs, but unfortunately you don't have the code anymore so we can verify that assertion. As was written earlier, combining random variables can have counter-intuitive effects, so maybe this might have caused your code to misbehave.

I find it odd that you are capable of writing and understanding such a program, but don't seem to know much about information theory, statistics, probability theory and such. That makes it really hard to believe you, but I believe we are still open to change our minds if you provide proof.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Random number?

Post by Plu »

That's a common misknowledge, but no, actually. I have posted exactly the function used in every OS to generate pseudo-random number just a few posts ago.
You mean this implementation?

Code: Select all

#define RAND_MAX 0x00010000 /* that would be 65536 */
static unsigned int seed = 1;
void srand ( int s )
{
  seed = s;
}

unsigned int rand ( void )
{
  seed = seed * 0x00FACEXD + 0xDEADBEEF;
  return seed % RAND_MAX;
}
Because that's a long and entirely deterministic list, just as Micha said. If you put in the same input, you get the same output. That's exactly what "deterministic" means. The fact that the list is infinite in length doesn't change the fact that there is only one list, and that if you start at the same place twice, you'll get the same results. That's why you have to seed the thing in the first place.
batatinha
Prole
Posts: 22
Joined: Mon Jun 24, 2013 3:49 am

Re: Random number?

Post by batatinha »

micha wrote: I am still curious to see an example, where patterns become visible.
Curiously i was recently working on a particles system made with LuaGL that uses a lot of math.random, and the effect was always the same, i already deleted that code because it's ugly, but i made a code to exemplify that and to see if randomseed helps or no:
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
lua: random.lua:15: '<name>' expected near '1'
>Exit code: 1
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1generated 987 times
Number 2generated 1005 times
Number 3generated 955 times
Number 4generated 978 times
Number 5generated 996 times
Number 6generated 984 times
Number 7generated 1062 times
Number 8generated 1001 times
Number 9generated 1014 times
Number 10generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers without any seed
Result:
Number 1 generated 987 times
Number 2 generated 1005 times
Number 3 generated 955 times
Number 4 generated 978 times
Number 5 generated 996 times
Number 6 generated 984 times
Number 7 generated 1062 times
Number 8 generated 1001 times
Number 9 generated 1014 times
Number 10 generated 1018 times
>Exit code: 0
There is a pattern...
But let's try this seeding with math.random():
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
Another pattern...

I will not try this with os.time because this only cover seconds, i'll seed with socket.gettime() instead:
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "random.lua"
Generating 10000 random numbers with random seed
Result:
Number 1 generated 967 times
Number 2 generated 967 times
Number 3 generated 987 times
Number 4 generated 1015 times
Number 5 generated 1036 times
Number 6 generated 995 times
Number 7 generated 1000 times
Number 8 generated 1016 times
Number 9 generated 1019 times
Number 10 generated 998 times
>Exit code: 0
Another visible pattern...
Here is the first source code:

Code: Select all

pattern = {}
pattern[1] = {}
pattern[2] = {}
pattern[3] = {}
pattern[4] = {}
pattern[5] = {}
pattern[6] = {}
pattern[7] = {}
pattern[8] = {}
pattern[9] = {}
pattern[10] = {}

function generatePattern(n)
	r = nil
	for a = 1,n do
		r = math.random(1, 10)
		table.insert(pattern[r], r)
	end
end

print('Generating 10000 random numbers without any seed')
generatePattern(10000)
print('Result:')
for k,v in pairs(pattern) do
	print('Number '.. k ..' generated '.. #v ..' times')
end
obs: if randomseed is setted on each for call, the result will be always 1.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Random number?

Post by micha »

raidho36 wrote:
PRNGs produce numbers from a long deterministic list. Setting the seed only changes the starting position within this list.
That's a common misknowledge, but no, actually. I have posted exactly the function used in every OS to generate pseudo-random number just a few posts ago.
+1 to what plu said (only small correction : the list is not infinite, but cyclic).

I just realize that the periodic length of the sequence is 65536. So if you generate one million programs with this sequence, then some of them have to be identical. In this case the random numbers fail because of the to small periodic length. Random seeding will not solve this, however.
raidho36 wrote:
if there really were patterns, then it should be fairly simple to write a program that shows these patterns, right?
Yes, just about every lossless compression algorithm uses dictionaries.
I don't understand, what you mean by this. Can you please explain?
batatinha wrote:...
In example no. 2, I believe the seed the you use is not random, because, as you proved in the first experiment, without a seed, random numbers are deterministic. So if you do something like randomSeed(random()) then this is deterministic as well.
For the third experiment, can you please run it once, then wait for some seconds and then run it again and show the output of these two runs?

By the way, the fact that you get the same numbers, if you start with the same seed, is a a property that is desired in some application (for reproducability reasons).
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Random number?

Post by vrld »

batatinha wrote:[WALL OF TEXT]
You didn't use a different seed at the beginning of each experiment, so of course the generated values are the same. I think raidho36 means we would be able to see patterns within one single run of the program, not across different runs.
Other than that, your experiment shows that math.random() indeed draws from a uniform distribution.
micha wrote:
raidho36 wrote:
PRNGs produce numbers from a long deterministic list. Setting the seed only changes the starting position within this list.
That's a common misknowledge, but no, actually. I have posted exactly the function used in every OS to generate pseudo-random number just a few posts ago.
+1 to what plu said (only small correction : the list is not infinite, but cyclic).
You (micha and raidho36) are both right: You will not only start from a different position, but you will also "shuffle" the list.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Locked

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 4 guests