Discussion:
%random% is not random?
(too old to reply)
Herbert Kleebauer
2023-10-21 16:09:03 UTC
Permalink
If I execute the following batch:

@echo off
echo %random% %random% %random% %random% %random% %random%
pause

by double clicking in explorer, I get these results:

32421 2119 21482 14360 29755 3072
32464 10777 24341 32271 4 21351
32529 29137 21176 22017 9688 1582
32575 15774 9130 31224 23020 23788
32621 2412 29853 7662 3584 13226
32660 321 14847 1510 29055 27579

The first use of %random% always gives nearly the
same value. Is this a Microsoft feature or a bug?
R.Wieser
2023-10-21 18:32:43 UTC
Permalink
Herbert,
Post by Herbert Kleebauer
The first use of %random% always gives nearly the
same value. Is this a Microsoft feature or a bug?
"CMD.EXE initializes the random number generator upon startup using a seed
that is derived from the current time with 1 second resolution"

https://stackoverflow.com/questions/19694021/random-generator-in-the-batch

Regards,
Rudy Wieser
Herbert Kleebauer
2023-10-21 20:10:28 UTC
Permalink
Post by R.Wieser
Herbert,
Post by Herbert Kleebauer
The first use of %random% always gives nearly the
same value. Is this a Microsoft feature or a bug?
"CMD.EXE initializes the random number generator upon startup using a seed
that is derived from the current time with 1 second resolution"
https://stackoverflow.com/questions/19694021/random-generator-in-the-batch
That would only mean, that all CMD startet in the same second
will have the same random number sequence. But a cmd startet
a few seconds later should start with a completely different
value. So as a work around, always discard the first %random%
value.
R.Wieser
2023-10-22 05:31:30 UTC
Permalink
Herbert,
Post by Herbert Kleebauer
That would only mean, that all CMD startet in the same second
will have the same random number sequence.
Thats the problem the poster "Val" started with : "Two consoles are opened
and both contain the same number, 4645." Also see the "-- OUTPUT 1 --"
list.
Post by Herbert Kleebauer
But a cmd startet a few seconds later should start with a completely
different value.
Not /completely/ different (your initial problem), but different enough to
create a different sequence.
Post by Herbert Kleebauer
So as a work around, always discard the first %random%
value.
That will work for your initial problem.

Regards,
Rudy Wieser
Herbert Kleebauer
2023-10-22 07:52:52 UTC
Permalink
Post by R.Wieser
Post by Herbert Kleebauer
But a cmd startet a few seconds later should start with a completely
different value.
Not /completely/ different (your initial problem), but different enough to
create a different sequence.
They use the seed not as a seed but as the first
random number and that I would call a bug. So
always discard the first %random% value you get.
R.Wieser
2023-10-22 08:13:53 UTC
Permalink
Herbert,
Post by Herbert Kleebauer
They use the seed not as a seed but as the first
random number and that I would call a bug.
At least two points of view :

1) Is the number random enough to be used ? Than what does it matter ?

2) Its outputting the seed as the first number so you know where the
sequence comes from, and can possibly re-seed the random generator to create
the same sequence.
Post by Herbert Kleebauer
So always discard the first %random% value you get.
As you do not like that the first returned value seems to fall in a certain
range (in your short timeframe) than that certainly seems to be a workable
fix.

Regards,
Rudy Wieser
Herbert Kleebauer
2023-10-22 08:49:55 UTC
Permalink
Post by R.Wieser
Post by Herbert Kleebauer
They use the seed not as a seed but as the first
random number and that I would call a bug.
1) Is the number random enough to be used ? Than what does it matter ?
If you put this batch in the autostart folder, you can
use an old Laptop as a digital picture frame. After
power on, it displays the pictures in the directory
%p% in a fixed order (not random), but starting at
a random position. If you switch on the Laptop every
day at nearly the same time, you will always see the
same pictures. I wouldn't call this "random enough".
Therefore the otherwise meaningless line:
set r=%random% %random% %random%



@echo off
:: timeout /t 10
set p=d:\test\tmp2
set iv=D:\Programme\IrfanView\i_view32.exe

cd /d %p%
set n=0
if exist list.txt del list.txt
for %%i in (*.jpg) do (
set /a n=n+1
echo %p%\%%i>>list.txt)
set r=%random% %random% %random%
set /a n=%random%*(n-1)/32767
more +%n% list.txt>list1.txt
copy /b list1.txt + list.txt
%iv% /slideshow=%p%\list1.txt
R.Wieser
2023-10-22 09:48:07 UTC
Permalink
Herbert,
If you switch on the Laptop every day at nearly the same time, you will
always see the same pictures. I wouldn't call this "random enough".
In that case skipping the first result won't help much, if at all. The
second number might *look* random, but isn't - as its calculated from the
first. You just get a /different/ set of numbers.

Look at it like this : Assuming you are always switching your laptop on in
the same 15 minute timeframe. By using the first result you get 15*60 = 900
different(?) results. If you use the second result than you will again only
get 900 different(?) ones. They will just be spread more thruout the full
range.

I used "(?)" because 'random' doesn't mean there can't be duplicate results.

Regards,
Rudy Wieser
Herbert Kleebauer
2023-10-22 10:44:38 UTC
Permalink
Post by R.Wieser
Herbert,
If you switch on the Laptop every day at nearly the same time, you will
always see the same pictures. I wouldn't call this "random enough".
In that case skipping the first result won't help much, if at all. The
second number might *look* random, but isn't - as its calculated from the
first. You just get a /different/ set of numbers.
Look at it like this : Assuming you are always switching your laptop on in
the same 15 minute timeframe. By using the first result you get 15*60 = 900
different(?) results. If you use the second result than you will again only
get 900 different(?) ones. They will just be spread more thruout the full
range.
You will get 900 different values, but in the form n, n+1, n+2, n+3 ...

Lets assume, there are 32768 pictures and you watch 1000 pictures and
the "random" numbers you get are 4000, 4001, ....4900, then the only
pictures you ever will see are pictures 4000-5900. But if you use
the second %random% value, you will see all 32768 pictures.

It is a bug to use the seed as the first random number!
R.Wieser
2023-10-22 13:23:25 UTC
Permalink
Herbert,
Post by Herbert Kleebauer
You will get 900 different values, but in the form n, n+1, n+2, n+3 ...
Thats odd, as that isn't even what "-- OUTPUT 1 --" in the link show - and
that is just three seconds worth.
Post by Herbert Kleebauer
But if you use the second %random% value, you will see all 32768 pictures
No, you won't.

Simply said : how do you think that if you put 900 different seconds in
(generating 900 different first %random% results) that you than can get
32768 different second %random% results from those ?
Post by Herbert Kleebauer
It is a bug to use the seed as the first random number!
In your particular use-case ? Not at all.

I'm even wondering why you think you should need to use a random value to
begin with.

Regards,
Rudy Wieser
Herbert Kleebauer
2023-10-22 10:46:10 UTC
Permalink
Post by R.Wieser
Herbert,
If you switch on the Laptop every day at nearly the same time, you will
always see the same pictures. I wouldn't call this "random enough".
In that case skipping the first result won't help much, if at all. The
second number might *look* random, but isn't - as its calculated from the
first. You just get a /different/ set of numbers.
Look at it like this : Assuming you are always switching your laptop on in
the same 15 minute timeframe. By using the first result you get 15*60 = 900
different(?) results. If you use the second result than you will again only
get 900 different(?) ones. They will just be spread more thruout the full
range.
You will get 900 different values, but in the form n, n+1, n+2, n+3 ...

Lets assume, there are 32768 pictures and you watch 1000 pictures and
the "random" numbers you get are 4000, 4001, ....4900, then the only
pictures you ever will see are pictures 4000-5900. But if you use
the second %random% value, you will see all 32768 pictures.

It is a bug to use the seed as the first random number!
R.Wieser
2023-10-22 09:59:47 UTC
Permalink
Herbert,
If you switch on the Laptop every day at nearly the same time, you will
always see the same pictures.
In the above use-case you could perhaps take a look at the last two digits
of the "time" command. Its the hundreds of a second, which for your "at
switch on time" application might as well be random. If 100 different
results is not enough you could always add (some of) the lower seconds
digit.

Another solution is to calculate the random value yourself (I think you
might already have seen some example code for it) and save the resulting
value in a file, so it can be used next time you need one.

Regards,
Rudy Wieser
R.Wieser
2023-10-22 18:07:16 UTC
Permalink
Herbert,
Post by Herbert Kleebauer
more +%n% list.txt>list1.txt
copy /b list1.txt + list.txt
I hope you realize that that method just "cuts the cards", and doesn't
change anything about the order of the files in the two parts. IOW, you are
only changing the starting image, and than continue in the same order as
yesterday and all the days before it.

... if it where not for the issue that you take a partial copy from
"list.txt" and put it into "list1.txt", *but are not removing* that part
from "list.txt"

IOW, all the files in "list1.txt" will be present *twice* in the end
result - once at the start and once at the end.

A visual explanation :

Contents of "list.txt" : 1,2,3,4,5,6,7,8,9

Cut at number 6.

"list1.txt" will contain : 6,7,8,9

The end result (list1.txt + list.txt) is : 6,7,8,9 + 1,2,3,4,5,6,7,8,9

But I don't think you will notice that much, in the light of the problem
that all images but two (the 9-to-1 in the middle and the 9-to-6 at the
end-to-start) are *always being shown in the same order*.

... which might just be what your posts question is actually all about.

Regards,
Rudy Wieser
Herbert Kleebauer
2023-10-22 19:08:22 UTC
Permalink
Post by R.Wieser
Contents of "list.txt" : 1,2,3,4,5,6,7,8,9
Cut at number 6.
"list1.txt" will contain : 6,7,8,9
The end result (list1.txt + list.txt) is : 6,7,8,9 + 1,2,3,4,5,6,7,8,9
But I don't think you will notice that much, in the light of the problem
that all images but two (the 9-to-1 in the middle and the 9-to-6 at the
end-to-start) are *always being shown in the same order*.
There is a time line, so they have to be displayed in the given
order, but the start of the sequence should be random. But if the
starting point is near the end of the sequence, then there are not
enough pictures in list1.txt, so the picture show would be finished
before the PC ist switched off. Therefore the complete file list
is appended to list1.txt. But the number of pictures displayed in
one session is always much lower than the total number of pictures.
R.Wieser
2023-10-22 20:03:11 UTC
Permalink
Herbert,
Post by Herbert Kleebauer
There is a time line, so they have to be displayed in the given
order, but the start of the sequence should be random.
...
Post by Herbert Kleebauer
But the number of pictures displayed in one session is always much lower
than the total number of pictures.
In that case that concatenation will certainly work.

So, back to the %random% value.

Yes, in that case using the second %random% value will give a better
"random" (read: non-sequential) starting position than the first %random%
value.

Regards,
Rudy Wieser
R.Wieser
2023-10-22 20:03:11 UTC
Permalink
Herbert,
Post by Herbert Kleebauer
There is a time line, so they have to be displayed in the given
order, but the start of the sequence should be random.
...
Post by Herbert Kleebauer
But the number of pictures displayed in one session is always much lower
than the total number of pictures.
In that case that concatenation will certainly work.

So, back to the %random% value.

Yes, in that case using the second %random% value will give a better
"random" (read: non-sequential) starting position than the first %random%
value.

Regards,
Rudy Wieser
R.Wieser
2023-10-23 07:18:39 UTC
Permalink
Herbert,
Post by Herbert Kleebauer
There is a time line, so they have to be displayed in the given
order, but the start of the sequence should be random.
...
Post by Herbert Kleebauer
But the number of pictures displayed in one session is always much lower
than the total number of pictures.
In that case that concatenation will certainly work.


So, back to the %random% value.

Yes, in that case using the second %random% value will give a better
"random" (read: non-sequential) starting position than the first %random%
value.

Though as mentioned before, the second on which you switch your machine on
will probably be "random" enough to suffice. Than again, that value is
harder to extract (from %time%) than grabbing the second %random% result.
:-)

A remark though:
Do remember that those second %random% values are not actually random. If,
for some combination of total available and displayed-in-a-session images an
image isn't presented (a session ends before the next starting point) it
never will.

Regards,
Rudy Wieser
R.Wieser
2023-10-23 07:24:57 UTC
Permalink
Although trying to post the message twice yesterday and see it timing-out
both times it seems that both actually went thru. :-\

The third one is just yesterdays post complemented with this mornings
ponderings.
Tom Del Rosso
2023-11-27 07:11:06 UTC
Permalink
Post by Herbert Kleebauer
That would only mean, that all CMD startet in the same second
will have the same random number sequence. But a cmd startet
a few seconds later should start with a completely different
value. So as a work around, always discard the first %random%
value.
I found that strategy to be ineffective.

This snippet from one of my files gets a truly random number called
"target" within the range from 1 to "total". If the website returns
nothing or a non-number then it falls back to the cmd.exe random number
generator.

LINE: rem get a truly random number from random.org
LINE: set "target="
LINE: for /f %%a in ('wget -q -O -
"https://www.random.org/integers/?num=1&min=1&max=%total%&col=1&base=10&format=plain&rnd=new"')
do set "target=%%a"
LINE:
LINE: rem get a pseudo random number in case random.org is offline
LINE: if not defined target set target=invalid
LINE: for /f "delims=0123456789 " %%a in ("%target%.") do (
LINE: if not "%%a"=="." (
LINE: echo random.org is offline. using pseudo random
LINE: set /a target = %random% * total / 32768 + 1
LINE: )
LINE: )
--
Defund the Thought Police
Kenny McCormack
2023-11-27 08:03:21 UTC
Permalink
In article <uk1fdd$3lva1$***@dont-email.me>,
Tom Del Rosso <***@that-google-mail-domain.com> wrote:
...
wget -q -O- "https://www.random.org/integers/?num=1&min=1&max=%total%&col=1&base=10&format=plain&rnd=new"

That's cute. Thanks for the tip!
--
"He is exactly as they taught in KGB school: an egoist, a liar, but talented - he
knows the mind of the wrestling-loving, under-educated, authoritarian-admiring
white male populous."
- Malcolm Nance, p59. -
Mr. Man-wai Chang
2023-12-18 15:15:56 UTC
Permalink
Post by Kenny McCormack
...
wget -q -O- "https://www.random.org/integers/?num=1&min=1&max=%total%&col=1&base=10&format=plain&rnd=new"
That's cute. Thanks for the tip!
But then why do you trust random.org? :)
Tom Del Rosso
2024-01-11 00:20:23 UTC
Permalink
Post by Mr. Man-wai Chang
Post by Kenny McCormack
...
wget -q -O-
"https://www.random.org/integers/?num=1&min=1&max=%total%&col=1&base=10&format=plain&rnd=new"
That's cute. Thanks for the tip!
But then why do you trust random.org? :)
I had test code to graph the results, but I deleted it.

The graph makes it perfectly clear that random.org is random and the
random e-variable is not. I said this before.
--
Defund the Thought Police
Herbert Kleebauer
2023-11-27 08:42:08 UTC
Permalink
Post by Tom Del Rosso
Post by Herbert Kleebauer
That would only mean, that all CMD startet in the same second
will have the same random number sequence. But a cmd startet
a few seconds later should start with a completely different
value. So as a work around, always discard the first %random%
value.
LINE: rem get a pseudo random number in case random.org is offline
LINE: if not defined target set target=invalid
LINE: for /f "delims=0123456789 " %%a in ("%target%.") do (
LINE: if not "%%a"=="." (
LINE: echo random.org is offline. using pseudo random
LINE: set /a target = %random% * total / 32768 + 1
Then you have the same problem: the first %random% isn't
random. So better insert a "dummy=%random% at the start of
the batch.
Tom Del Rosso
2023-11-27 15:50:54 UTC
Permalink
Post by Herbert Kleebauer
Then you have the same problem: the first %random% isn't
random. So better insert a "dummy=%random% at the start of
the batch.
In an earlier version it saved a log with a graph output to show the
pattern. It revealed that the second number wasn't very random either.
--
Defund the Thought Police
Herbert Kleebauer
2023-11-27 17:03:52 UTC
Permalink
Post by Tom Del Rosso
Post by Herbert Kleebauer
Then you have the same problem: the first %random% isn't
random. So better insert a "dummy=%random% at the start of
the batch.
In an earlier version it saved a log with a graph output to show the
pattern. It revealed that the second number wasn't very random either.
It is a pseudo random generator, the second value depends on
the first value.

@echo off
for /l %%i in (1,1,20) do (
cmd /c echo %%random%% %%random%% %%random%% %%random%%
timeout /t 1 >nul)

But the second value of %random% at least looks much more
random than the first one:

29515 13600 11704 18330
29518 24348 29568 9626
29521 2328 14664 921
29525 13077 32529 24984
29528 23825 17625 16280
29531 1806 2721 7575
29534 12554 20585 31638
29538 23302 5681 22934
29541 1283 23545 14229
29544 12031 8642 5524
29548 22780 26506 29588
29551 760 11602 20883
29554 11508 29466 12178
29557 22257 14562 3473
29561 237 32426 27537
29564 10986 17523 18832
29567 21734 2619 10127
29570 32483 20483 1423
29574 10463 5579 25486
29577 21211 23443 16781
JJ
2023-11-28 01:18:17 UTC
Permalink
Post by Herbert Kleebauer
It is a pseudo random generator, the second value depends on
the first value.
@echo off
for /l %%i in (1,1,20) do (
cmd /c echo %%random%% %%random%% %%random%% %%random%%
timeout /t 1 >nul)
But the second value of %random% at least looks much more
29515 13600 11704 18330
29518 24348 29568 9626
29521 2328 14664 921
29525 13077 32529 24984
29528 23825 17625 16280
29531 1806 2721 7575
29534 12554 20585 31638
29538 23302 5681 22934
29541 1283 23545 14229
29544 12031 8642 5524
29548 22780 26506 29588
29551 760 11602 20883
29554 11508 29466 12178
29557 22257 14562 3473
29561 237 32426 27537
29564 10986 17523 18832
29567 21734 2619 10127
29570 32483 20483 1423
29574 10463 5579 25486
29577 21211 23443 16781
Interresting. It seems like the first use of the randomizer is like using
timestamp solely for the random seed.
R.Wieser
2023-11-28 06:59:25 UTC
Permalink
JJ,
Post by JJ
Interresting. It seems like the first use of the randomizer is
like using timestamp solely for the random seed.
Yup. But why do they output it ?

I thought it would be so you could use it to re-create a certain "random"
set of numbers, but I do not see any way to manually set the seed.

Regards,
Rudy Wieser
Tom Del Rosso
2023-12-03 17:54:25 UTC
Permalink
Post by Herbert Kleebauer
Post by Tom Del Rosso
In an earlier version it saved a log with a graph output to show the
pattern. It revealed that the second number wasn't very random either.
But the second value of %random% at least looks much more
The second is *slightly* more random. In groups of 3 they just oscillate
up and down, with the upper and lower peaks almost equal in every 3rd
number.

Make a graph and it looks like a terrible excuse for randomness.

Graph the output of random.org and it looks great.
--
Defund the Thought Police
Loading...