Options

Random Number

madmmadm Member Posts: 92
edited 2011-12-06 in Navision Attain
I have managed to create a "random" number field, simply by

RANDOMIZE;
RndmNumber := RANDOM(12);

Which gives me a number between 1 and 12. How do I give it a minimum? ( I only want 8 - 12) ?

Thanks :)

Answers

  • Options
    SunsetSunset Member Posts: 201
    You can't do minimum but ...

    RndmNumber := RANDOM(4) + 8 should do nicely :lol:
    Don't just take my word for it, test it yourself
  • Options
    madmmadm Member Posts: 92
    Sunset wrote:
    You can't do minimum but ...

    RndmNumber := RANDOM(4) + 8 should do nicely :lol:

    yours was the easy approach lol ;)

    i came up with a long way, not sure if its acceptable?!

    IF RndmNumber <8 THEN
    REPEAT
    RANDOMIZE;
    RndmNumber := RANDOM(12);
    UNTIL RndmNumber >7;
  • Options
    jlandeenjlandeen Member Posts: 524
    I completely agree with sunset's approach.

    I've found in the past that Navision's random number generator is not as "random" as I would have expected. This could mean that you're loop may end up repeating too many times if there are a lot of random numbers generated that are less then or equal to 7.
    Jeff Landeen - Sr. Consultant
    Epimatic Corp.

    http://www.epimatic.com
  • Options
    DenSterDenSter Member Posts: 8,304
    Sunset wrote:
    You can't do minimum but ...

    RndmNumber := RANDOM(4) + 8 should do nicely :lol:
    =D> =D>
    Excellence in simplicity, I like that one :mrgreen:
  • Options
    kinekine Member Posts: 12,562
    This way to generate numbers is very similar in all languages. Some languages had just random generator generating numbers between 0 and 1. It means, if you wanted to have numbers from range 10..90 you needed to do
    I := RANDOM*80+10;

    8)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    madmmadm Member Posts: 92
    thanks :)

    i did think that the loop may end up looping continually if the random number never went over 7!!

    So went for

    RANDOM(5) + 7; (so that 8 is given a chance)!
  • Options
    btilmanbtilman Member Posts: 5
    So how can I get this code into a Text field
  • Options
    SunsetSunset Member Posts: 201
    btilman wrote:
    So how can I get this code into a Text field

    The easy way round this would be to use FORMAT.
    Have a function create the random number you want, and then FORMAT(Number)
    Don't just take my word for it, test it yourself
Sign In or Register to comment.