RandomNumber - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

RandomNumber

How Do You Do A Random Number Out Of Your Own Selection Of Numbers, For Example You Have 1, 2, 3 and u want the computer to choose one of the 3 at random.


Comments

  • How about...

    X:=Random(3)+1; { returns random number between 1..3}



    ...or maybe you want to print a random string ?

    var s:array[1..3] of string;

    begin

    s[1]:='string1';

    s[2]:='abc';

    s[3]:='xyz';

    X:=Random(3)+1;

    writeln(s[x]);



    ...or maybe you want to print a random value ?

    var s:array[1..3] of integer;

    begin

    s[1]:=-50;

    s[2]:=1000;

    s[3]:=666;

    X:=Random(3)+1;

    writeln(s[x]);



    Does this help ?

    Re Lance



    : How Do You Do A Random Number Out Of Your Own Selection Of Numbers, For Example You Have 1, 2, 3 and u want the computer to choose one of the 3 at random.

    :








  • Make sure you put the "randomize" procedure in before getting the number.



    : How about...

    : X:=Random(3)+1; { returns random number between 1..3}

    :

    : ...or maybe you want to print a random string ?

    : var s:array[1..3] of string;

    : begin

    : s[1]:='string1';

    : s[2]:='abc';

    : s[3]:='xyz';

    : X:=Random(3)+1;

    : writeln(s[x]);

    :

    : ...or maybe you want to print a random value ?

    : var s:array[1..3] of integer;

    : begin

    : s[1]:=-50;

    : s[2]:=1000;

    : s[3]:=666;

    : X:=Random(3)+1;

    : writeln(s[x]);

    :

    : Does this help ?

    : Re Lance

    :

    : : How Do You Do A Random Number Out Of Your Own Selection Of Numbers, For Example You Have 1, 2, 3 and u want the computer to choose one of the 3 at random.

    : :

    :

    :

    :






  • :

    : Make sure you put the "randomize" procedure in before getting the number.





    what is the randomize procedure?




  • The randomize procedure gets the current tick of your computer's internal timer (or something like that) and then sets it as a variable so when you call random(), the program has some sort of a number to put through several formulas.



    Well anyway, it is because there is really no such thing as just "random" (ie. there is always some factor of why a particular number has been chosen).


  • Radomize simply takes the already chose number and throws it back into the hat... if u dont use randomize the random number will stay they same until u restart the program... so u can use it both ways depending on ur preference.


Sign In or Register to comment.