Recent

Author Topic: A begginer question.  (Read 9876 times)

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: A begginer question.
« Reply #15 on: June 07, 2017, 09:39:19 pm »
@thaddy :
we are talking about 2 diferent things here
1/ im talking about the results of the guy in the blog and the way he is formulating the problem it self
2/ you are talking about the fluctuations in the results of the programs ...
with 2 i do not have any issues ... i want your opinion on 1

the guy is talking of a squre 1x1 and illustarates idea in the quadrant 1 ... but in reality he is assuming than length can be negative value witch seems a little bit weired to me because its a Line segment not a vector
Speak postscript or die!
Translate to pdf and live!

Thaddy

  • Hero Member
  • *****
  • Posts: 18684
  • Jungle wars. And failing health it seems.
Re: A begginer question.
« Reply #16 on: June 07, 2017, 09:52:07 pm »
@thaddy :
we are talking about 2 diferent things here
1/ im talking about the results of the guy in the blog and the way he is formulating the problem it self
Make the integers cardinal....
I am sorry about my firm reaction, but the random is a positive value. Old School Pascal did not have a Cardinal or UInt type, hence the - by now - mistaken use of integer.
FPC's random can when cast to an integer (so even by assigning to an integer!), obtain negative values (high bit set) , but not when the proper type is used. Does that meet your standards?
« Last Edit: June 07, 2017, 09:55:28 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: A begginer question.
« Reply #17 on: June 07, 2017, 10:06:20 pm »
yes random function returns an extended in the range >=0 and  <1  but consider the result of  (random - random) ...it' s >-1 and <1 thats all im saying ... its not normal to have a negative value for distance ( delta x) unless it s a vector calculations

Speak postscript or die!
Translate to pdf and live!

Thaddy

  • Hero Member
  • *****
  • Posts: 18684
  • Jungle wars. And failing health it seems.
Re: A begginer question.
« Reply #18 on: June 07, 2017, 10:18:02 pm »
yes random function returns an extended in the range >=0 and  <1  but consider the result of  (random - random) ...it' s >-1 and <1 thats all im saying ... its not normal to have a negative value for distance ( delta x) unless it s a vector calculations
But that IS programmer error. Use ABS(), because it is distance...

Actually, the internal random is just a 32 bit value over the whole 32 bit range.
The float part is just scaling.
In my threadsafe fpc compatible  random I use:
Code: Pascal  [Select][+][-]
  1. const
  2.   // conversion constants. Converts URAND32 output from Cardinal to Float.
  3.   CONVERT_UNSIGNED = Extended (1.0/int64(1 shl 32)); //  0..1
  4.   CONVERT_SIGNED   = Extended (2.0/int64(1 shl 32)); // -1..1
  5.  

(Note that the int64 cast is really needed to obtain 32 bit precision in any meaningful way without assembler)
« Last Edit: June 07, 2017, 10:33:06 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: A begginer question.
« Reply #19 on: June 07, 2017, 10:47:59 pm »
offtopic : @thaddy : where can i get your urand32 implenentation because i need to generate *really* random sessions id across several nodes of a backend without to have to check if it exists in a shared db?... i do not like the idea of using guids
Speak postscript or die!
Translate to pdf and live!

MSABC

  • New Member
  • *
  • Posts: 43
Re: A begginer question.
« Reply #20 on: June 07, 2017, 10:49:04 pm »
@MarkoRadoicic:

with a variing value like "round(100000*double(Time()));"
instead of "randomize" you will get variing random numbers
(as said by Thaddy)

Nevertheless, the results will vary around 516, not around 521.

The reason is that you try to calculate with 100x100 numbers but
actually have only 99x99.

This is caused by your while-loops:
Code: Pascal  [Select][+][-]
  1. Count := 1;
  2.     while Count < 100 do
  3.     begin
  4.       //something
  5.       Count := Count + 1;
  6.     end;

will run fo Count := 1 to Count := 99, thus 99 times
Code: Pascal  [Select][+][-]
  1.  while Count <= 100 do
will run 100 times.



Thaddy

  • Hero Member
  • *****
  • Posts: 18684
  • Jungle wars. And failing health it seems.
Re: A begginer question.
« Reply #21 on: June 07, 2017, 10:57:19 pm »
offtopic : @thaddy : where can i get your urand32 implenentation because i need to generate *really* random sessions id across several nodes of a backend without to have to check if it exists in a shared db?... i do not like the idea of using guids

Wait a few days. I have a randoms package that offers:
- thread safety for FPC's Mersenne Twister
- delphi compatibility random
- C++/Java/C#  compatibility
- Cryptographically safe randoms
- Superfast PRNG's
Algorithms. All for different purposes.

If you need it NOW and cross platform use the random in LockBox,,,That comes close.
If you need it just on Windows, use the crypto API.

My code will abstract that for you. Most of it is already published by me (here, wiki and wikipedia) but not yet ready for release. It is all fresh code and meets all tests.

"Old"  examples:
https://en.wikipedia.org/wiki/Mersenne_Twister#Freepascal_implementation
http://wiki.freepascal.org/A_simple_implementation_of_the_Mersenne_twister
http://wiki.freepascal.org/Marsaglia%27s_pseudo_random_number_generators

These are actually all recent,but I want to do it properly. And it is all my code, nothing begged stealed or borrowed.

« Last Edit: June 07, 2017, 11:06:49 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: A begginer question.
« Reply #22 on: June 07, 2017, 11:06:23 pm »
thanks! will wait... no rush..as ucan imagine i do not have  zillions of users waiting for ids :)). now im using crypted as jwt utime + node ip ... but wantto add some randomness
Speak postscript or die!
Translate to pdf and live!

Thaddy

  • Hero Member
  • *****
  • Posts: 18684
  • Jungle wars. And failing health it seems.
Re: A begginer question.
« Reply #23 on: June 07, 2017, 11:08:19 pm »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

MarkoRadoicic

  • Newbie
  • Posts: 4
Re: A begginer question.
« Reply #24 on: June 07, 2017, 11:08:50 pm »
Thanks everyone. I feel so dumb :( 100 - x1 - x2 =/= x1 - x2. Anyways, I am certainly going to have to study all your answers deeply and thoroughly since I have no idea what half of them mean :P
I suppose I'm off to googling now. I might ask some questions about the answers later though.

Thanks in advance guys.

MarkoRadoicic

  • Newbie
  • Posts: 4
Re: A begginer question.
« Reply #25 on: June 07, 2017, 11:18:49 pm »
here is my answer
no need for any arrays or fancy calculations :)))
but there is something wrong with the results of the guy with the blog ... 0.521 is the result if you calculate even negative  dx and dy ... witch is not correct
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);

var Acc,dx,dy: Double;
      i: Integer;
const Iterrations=1000000;
begin
   Acc:=0;
   for i:=1 to Iterrations do
    begin
     dx:=abs(Random-random);
     dy:=abs(Random-random);
     Acc:=Acc+(sqrt(sqr(dx)+sqr(dy)));
    end;
   Acc:=Acc / Iterrations ;

   Label1.Caption:=FloatToStr(Acc);
end;             

 Well this was quick. What does TForm1.Button1Click(Sender: TObject) mean? I've never seen something like this :(

 This too:   Label1.Caption:=FloatToStr(Acc) is there a wiki page containing information about these two? I was unable to find one.

 I apologise if these are dumb questions. I just really want to know this stuff.

Bart

  • Hero Member
  • *****
  • Posts: 5666
    • Bart en Mariska's Webstek
Re: A begginer question.
« Reply #26 on: June 07, 2017, 11:29:59 pm »
It's done in Lazarus.
The code is run in the OnClick event-hander of Button1.

Bart

 

TinyPortal © 2005-2018