Recent

Author Topic: [SOLVED] Syntax error ("." expected) using Random  (Read 4004 times)

slender

  • Newbie
  • Posts: 4
[SOLVED] Syntax error ("." expected) using Random
« on: December 15, 2015, 10:32:45 pm »
Hi guys (and sorry for my english),
i'm new to FPC, so my question might be a little dumb.

I need to generate a random number (and using RandSeed (which is just a timestamp) is not random enough for me). I've found a function Random with syntax Random(l:integer), but when I write this code:

program random;

var
  x:integer;

begin
  Randomize;
  x := Random;
  writeln(x);
end.
     

the build fails with this error:

random.pas(8,14) Fatal: Syntax error, "." expected but ";" found

What did I do wrong?
« Last Edit: December 15, 2015, 10:51:00 pm by slender »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Syntax error ("." expected) using Random
« Reply #1 on: December 15, 2015, 10:44:40 pm »
The reason this error is presented is that you call the function random from unit system  (or at least i assume that is that is your intention).

But because your program is also named random, the compiler thinks you want to access a definition from your program.

(try replacing the line x := Random; with x := Random.x; (it magically works  ;) )

To solve it either:
- rename your program e.g. to random1
- replace the line with the call to the random function to System.Random

Do note however that your use of the random function is incorrect, so you are presented with yet another error when trying to compile, see also here.

edit: typos removed
« Last Edit: December 15, 2015, 10:48:41 pm by molly »

MSABC

  • New Member
  • *
  • Posts: 43
Re: Syntax error ("." expected) using Random
« Reply #2 on: December 15, 2015, 10:48:15 pm »
Your program name 'random' is the same as the function name 'random'.
This confuses the compiler.
If you rename the program eg. to 'rabdomprg', it will almost work.

To get it really working you also have to change line 3
from' x: integer'
to 'x:double'.
The random function returns a float value, no an integer.

Hope, I could help

MSABC



slender

  • Newbie
  • Posts: 4
Re: Syntax error ("." expected) using Random
« Reply #3 on: December 15, 2015, 10:50:08 pm »
Oh, thank you very much, that solved my problem. :)

Zaxxon

  • New Member
  • *
  • Posts: 30
Re: [SOLVED] Syntax error ("." expected) using Random
« Reply #4 on: December 16, 2015, 10:15:22 am »
Random returns Int64/Longint not double.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: [SOLVED] Syntax error ("." expected) using Random
« Reply #5 on: December 16, 2015, 12:23:58 pm »
Random returns Int64/Longint not double.
That's why, and when possible i put up a link to the documentation.

Quote
function Random( l: LongInt):LongInt;

function Random( l: Int64):Int64;

function Random: extended;
So, Random() returns a extended (that includes double), but when providing it an range value Random() returns a integer value.

I would not know what the OP slender had intended to be using, because he expected back an integer, yet didn't call random() with providing a parameter.

That is why i referred to a new error being presented ;)

Apparently OP realized that as well, as OP reported back to have solved the problem.

Zaxxon

  • New Member
  • *
  • Posts: 30
Re: [SOLVED] Syntax error ("." expected) using Random
« Reply #6 on: December 16, 2015, 12:51:13 pm »
I checked the systemh.inc file again and saw: Function  Random: extended;
missed that when I first checked. Too early in the morning I guess.

 

TinyPortal © 2005-2018