Pascal - Help - 5 Random numbers, not repeated?

Discussion in 'Software' started by sheepondrugs, Nov 21, 2007.

  1. sheepondrugs

    sheepondrugs Private First Class

    Hi, ive been trying to make a simple program, and for the past few days its been getting the better of me.rolleyes

    user presses [ENTER]
    computer shows 5 random numbers on the screen ( writeln(count) )
    the numbers wont be repeated once they have been picked.

    for example: number generated from 1 to 20:

    5 19 2 4 9



    Code:
      begin
      randomize;
    
       for count:=1 to 5 do     
    
         list[count] := random(13);  { random number 0 - 13}
    
              for count2 := 2 to 5 do
    
    			{ dont dupe number }
              end;
    
       end;
    

    I know what i need to do in english, but im in a pickle when it comes to writing it into the code.

    for 1 to 5 do
    generate number and add to array ( 1 to 5)
    is "number 2" the same as 3, 4, 5? (repeat for "number 3", 4, 5)
    if it is, ranomize again until it isnt the same as any other number.


    any help would be brilliant, thanks in advance! :)
     
  2. timduk

    timduk Private E-2

    How in Java:
    (Last did Pascal in 1970s...:))

    class Aclass {

    /* Constants - change these to suit your needs */
    static final int MAXRAND = 20;
    static final int NUMBERSWANTED = 5;

    /* Arrays - initialised by default to false & zero */
    boolean [] gotThisNumber = new boolean [MAXRAND];
    int [] numbers = new int [NUMBERSWANTED];

    public static void main( String [] args ) {
    for( int i = 0; i < NUMBERSWANTED; i++ ) {
    while( true ) {
    numbers = (int)(Math.random() * MAXRAND);
    if( gotThisNumber[numbers] ) continue;
    gotThisNumber[numbers] = true;
    break;
    }
    }
    for( int i = 0; i < NUMBERSWANTED; i++ )
    System.out.print( numbers + " " );
    }
    }

    The code could be tighter, but it shows the principle.
    PS.. sorry about lack of indents... how do you do that?
     
  3. sheepondrugs

    sheepondrugs Private First Class

    TAB key to indent :D
     
  4. timduk

    timduk Private E-2


    D'oh... :eek:
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds