Random - 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.

Random

NinthAngleNinthAngle Posts: 98Member
[b][red]This message was edited by NinthAngle at 2005-9-15 6:51:55[/red][/b][hr]
Ive been trying to create code that will randomly create a number and only display that number if it hasnt been shown before. This it does 5 times.

[code]
PROGRAM RandomNumbers;
USES Crt;
CONST MaxFileNames= 5;
VAR a: Array[1..5] of Integer;
b: Array[1..5] of Integer;
i, x, runs: Integer;

BEGIN
Repeat
ClrScr;
for i:=1 to 5 do {Define First Array}
Begin
a[i]:=i;
End;

Randomize; {Randomize}
Repeat
x:=Random(5)+1;
Until x <> b[1..5]; {Check}


Writeln(x); {Display}


b[runs]:=x; {Memorize}
Until runs=5;

END.
[/code]


I know the code is simply wrong in some areas, but thats as good a job i can do of communicating my idea...

Comments

  • GaashiusGaashius Posts: 349Member
    : [b][red]This message was edited by NinthAngle at 2005-9-15 6:51:55[/red][/b][hr]
    : Ive been trying to create code that will randomly create a number and only display that number if it hasnt been shown before. This it does 5 times.
    :
    : [code]
    : PROGRAM RandomNumbers;
    : USES Crt;
    : CONST MaxFileNames= 5;
    : VAR a: Array[1..5] of Integer;
    : b: Array[1..5] of Integer;
    : i, x, runs: Integer;
    :
    : BEGIN
    : Repeat
    : ClrScr;
    : for i:=1 to 5 do {Define First Array}
    : Begin
    : a[i]:=i;
    : End;
    :
    : Randomize; {Randomize}
    : Repeat
    : x:=Random(5)+1;
    : Until x <> b[1..5]; {Check}
    :
    :
    : Writeln(x); {Display}
    :
    :
    : b[runs]:=x; {Memorize}
    : Until runs=5;
    :
    : END.
    : [/code]
    :
    :
    : I know the code is simply wrong in some areas, but thats as good a job i can do of communicating my idea...
    :
    :
    Some optimizations:
    - You should delete all code about the [green]A[/green] array variable - it is useless. Therefore you should remove [green]I[/green] also.

    - You should put [blue]Randomize[/blue] outside the Repeat-Until loop.

    - You should use [blue]inc[/blue]([green]runs[/green][blue]);[/blue] before the Until.

    - You should remove the const [green]MaxFileNames[/green].

    Maybe you want something like this? :
    [code]
    var
    x,runs:byte;
    memo:array[1..5] of boolean;

    begin
    for x:=1 to 5 do memo[x]:=false;
    repeat
    x:=random(5)+1;
    if memo[x]=false then begin
    memo[x]=true; writeln(x);
    end;
    inc(runs);
    until runs=5;
    end;
    [/code]
    ****************
    Any questions? Just ask!

    :-) [b][blue]GAASHIUS[/blue][/b] :-)


  • NinthAngleNinthAngle Posts: 98Member
    : : [b][red]This message was edited by NinthAngle at 2005-9-15 6:51:55[/red][/b][hr]
    : : Ive been trying to create code that will randomly create a number and only display that number if it hasnt been shown before. This it does 5 times.
    : :
    : : [code]
    : : PROGRAM RandomNumbers;
    : : USES Crt;
    : : CONST MaxFileNames= 5;
    : : VAR a: Array[1..5] of Integer;
    : : b: Array[1..5] of Integer;
    : : i, x, runs: Integer;
    : :
    : : BEGIN
    : : Repeat
    : : ClrScr;
    : : for i:=1 to 5 do {Define First Array}
    : : Begin
    : : a[i]:=i;
    : : End;
    : :
    : : Randomize; {Randomize}
    : : Repeat
    : : x:=Random(5)+1;
    : : Until x <> b[1..5]; {Check}
    : :
    : :
    : : Writeln(x); {Display}
    : :
    : :
    : : b[runs]:=x; {Memorize}
    : : Until runs=5;
    : :
    : : END.
    : : [/code]
    : :
    : :
    : : I know the code is simply wrong in some areas, but thats as good a job i can do of communicating my idea...
    : :
    : :
    : Some optimizations:
    : - You should delete all code about the [green]A[/green] array variable - it is useless. Therefore you should remove [green]I[/green] also.
    :
    : - You should put [blue]Randomize[/blue] outside the Repeat-Until loop.
    :
    : - You should use [blue]inc[/blue]([green]runs[/green][blue]);[/blue] before the Until.
    :
    : - You should remove the const [green]MaxFileNames[/green].
    :
    : Maybe you want something like this? :
    : [code]
    : var
    : x,runs:byte;
    : memo:array[1..5] of boolean;
    :
    : begin
    : for x:=1 to 5 do memo[x]:=false;
    : repeat
    : x:=random(5)+1;
    : if memo[x]=false then begin
    : memo[x]=true; writeln(x);
    : end;
    : inc(runs);
    : until runs=5;
    : end;
    : [/code]
    : ****************
    : Any questions? Just ask!
    :
    : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    :
    :
    :

    I understand the idea behind you program. However, there seems to be somthing wrong with an essential part of the code. Whenever I try and execute the prog TP gives me an "error 91 ':=' expected" at the part where you try and set memo[x] as true. tp doesnt like the statement

    [code] memon[x]:=true [/code]

    how can i make [code] memo[x] [/code] true? There is no sample code under boolean in the TP help file.
  • GaashiusGaashius Posts: 349Member
    : : : [b][red]This message was edited by NinthAngle at 2005-9-15 6:51:55[/red][/b][hr]
    : : : Ive been trying to create code that will randomly create a number and only display that number if it hasnt been shown before. This it does 5 times.
    : : :
    : : : [code]
    : : : PROGRAM RandomNumbers;
    : : : USES Crt;
    : : : CONST MaxFileNames= 5;
    : : : VAR a: Array[1..5] of Integer;
    : : : b: Array[1..5] of Integer;
    : : : i, x, runs: Integer;
    : : :
    : : : BEGIN
    : : : Repeat
    : : : ClrScr;
    : : : for i:=1 to 5 do {Define First Array}
    : : : Begin
    : : : a[i]:=i;
    : : : End;
    : : :
    : : : Randomize; {Randomize}
    : : : Repeat
    : : : x:=Random(5)+1;
    : : : Until x <> b[1..5]; {Check}
    : : :
    : : :
    : : : Writeln(x); {Display}
    : : :
    : : :
    : : : b[runs]:=x; {Memorize}
    : : : Until runs=5;
    : : :
    : : : END.
    : : : [/code]
    : : :
    : : :
    : : : I know the code is simply wrong in some areas, but thats as good a job i can do of communicating my idea...
    : : :
    : : :
    : : Some optimizations:
    : : - You should delete all code about the [green]A[/green] array variable - it is useless. Therefore you should remove [green]I[/green] also.
    : :
    : : - You should put [blue]Randomize[/blue] outside the Repeat-Until loop.
    : :
    : : - You should use [blue]inc[/blue]([green]runs[/green][blue]);[/blue] before the Until.
    : :
    : : - You should remove the const [green]MaxFileNames[/green].
    : :
    : : Maybe you want something like this? :
    : : [code]
    : : var
    : : x,runs:byte;
    : : memo:array[1..5] of boolean;
    : :
    : : begin
    : : for x:=1 to 5 do memo[x]:=false;
    : : repeat
    : : x:=random(5)+1;
    : : if memo[x]=false then begin
    : : memo[x]=true; writeln(x);
    : : end;
    : : inc(runs);
    : : until runs=5;
    : : end;
    : : [/code]
    : : ****************
    : : Any questions? Just ask!
    : :
    : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : :
    : :
    : :
    :
    : I understand the idea behind you program. However, there seems to be somthing wrong with an essential part of the code. Whenever I try and execute the prog TP gives me an "error 91 ':=' expected" at the part where you try and set memo[x] as true. tp doesnt like the statement
    :
    : [code] memon[x]:=true [/code]
    :
    : how can i make [code] memo[x] [/code] true? There is no sample code under boolean in the TP help file.
    :
    Sorry, I have miswritten a thing in the code.
    [code]
    var
    x,runs:byte;
    memo:array[1..5] of boolean;

    begin
    for x:=1 to 5 do memo[x]:=false;
    repeat
    x:=random(5)+1;
    if memo[x]=false then begin
    memo[x]:=true; writeln(x);
    end;
    inc(runs);
    until runs=5;
    end;
    [/code]
    Now it should work.
    ****************
    Any questions? Just ask!

    :-) [b][blue]GAASHIUS[/blue][/b] :-)


  • NinthAngleNinthAngle Posts: 98Member
    : : : : [b][red]This message was edited by NinthAngle at 2005-9-15 6:51:55[/red][/b][hr]
    : : : : Ive been trying to create code that will randomly create a number and only display that number if it hasnt been shown before. This it does 5 times.
    : : : :
    : : : : [code]
    : : : : PROGRAM RandomNumbers;
    : : : : USES Crt;
    : : : : CONST MaxFileNames= 5;
    : : : : VAR a: Array[1..5] of Integer;
    : : : : b: Array[1..5] of Integer;
    : : : : i, x, runs: Integer;
    : : : :
    : : : : BEGIN
    : : : : Repeat
    : : : : ClrScr;
    : : : : for i:=1 to 5 do {Define First Array}
    : : : : Begin
    : : : : a[i]:=i;
    : : : : End;
    : : : :
    : : : : Randomize; {Randomize}
    : : : : Repeat
    : : : : x:=Random(5)+1;
    : : : : Until x <> b[1..5]; {Check}
    : : : :
    : : : :
    : : : : Writeln(x); {Display}
    : : : :
    : : : :
    : : : : b[runs]:=x; {Memorize}
    : : : : Until runs=5;
    : : : :
    : : : : END.
    : : : : [/code]
    : : : :
    : : : :
    : : : : I know the code is simply wrong in some areas, but thats as good a job i can do of communicating my idea...
    : : : :
    : : : :
    : : : Some optimizations:
    : : : - You should delete all code about the [green]A[/green] array variable - it is useless. Therefore you should remove [green]I[/green] also.
    : : :
    : : : - You should put [blue]Randomize[/blue] outside the Repeat-Until loop.
    : : :
    : : : - You should use [blue]inc[/blue]([green]runs[/green][blue]);[/blue] before the Until.
    : : :
    : : : - You should remove the const [green]MaxFileNames[/green].
    : : :
    : : : Maybe you want something like this? :
    : : : [code]
    : : : var
    : : : x,runs:byte;
    : : : memo:array[1..5] of boolean;
    : : :
    : : : begin
    : : : for x:=1 to 5 do memo[x]:=false;
    : : : repeat
    : : : x:=random(5)+1;
    : : : if memo[x]=false then begin
    : : : memo[x]=true; writeln(x);
    : : : end;
    : : : inc(runs);
    : : : until runs=5;
    : : : end;
    : : : [/code]
    : : : ****************
    : : : Any questions? Just ask!
    : : :
    : : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : : :
    : : :
    : : :
    : :
    : : I understand the idea behind you program. However, there seems to be somthing wrong with an essential part of the code. Whenever I try and execute the prog TP gives me an "error 91 ':=' expected" at the part where you try and set memo[x] as true. tp doesnt like the statement
    : :
    : : [code] memon[x]:=true [/code]
    : :
    : : how can i make [code] memo[x] [/code] true? There is no sample code under boolean in the TP help file.
    : :
    : Sorry, I have miswritten a thing in the code.
    : [code]
    : var
    : x,runs:byte;
    : memo:array[1..5] of boolean;
    :
    : begin
    : for x:=1 to 5 do memo[x]:=false;
    : repeat
    : x:=random(5)+1;
    : if memo[x]=false then begin
    : memo[x]:=true; writeln(x);
    : end;
    : inc(runs);
    : until runs=5;
    : end;
    : [/code]
    : Now it should work.
    : ****************
    : Any questions? Just ask!
    :
    : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    :
    :
    :

    hi,
    im not sure i understand this code. whenever I execute thge program, I simply get the thee numbers 2, 5 and 1. everytime. shouldnt the random(x) command be responsible for giving a different set of numbers every time. shouldnt that same set be 5 and not 3 numbers???

  • Phat NatPhat Nat Posts: 757Member
    : : : : : [b][red]This message was edited by NinthAngle at 2005-9-15 6:51:55[/red][/b][hr]
    : : : : : Ive been trying to create code that will randomly create a number and only display that number if it hasnt been shown before. This it does 5 times.

    : : [code]
    : : var
    : : x,runs:byte;
    : : memo:array[1..5] of boolean;
    : :
    : : begin
    : : for x:=1 to 5 do memo[x]:=false;
    : : repeat
    : : x:=random(5)+1;
    : : if memo[x]=false then begin
    : : memo[x]:=true; writeln(x);
    : : end;
    : : inc(runs);
    : : until runs=5;
    : : end;
    : : [/code]
    : : Now it should work.
    : : ****************
    : : Any questions? Just ask!
    : :
    : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : :
    : :
    : :
    :
    : hi,
    : im not sure i understand this code. whenever I execute thge program, I simply get the thee numbers 2, 5 and 1. everytime. shouldnt the random(x) command be responsible for giving a different set of numbers every time. shouldnt that same set be 5 and not 3 numbers???

    First off, if you don't put RANDOMIZE in your code (preferrably at the start, only needed once) you will get the same results everytime. Gaashius' code takes a random number and if it has been shown before it counts that as a run.
    There are a few ways that you could do it.
    You could generate an array of random numbers, making sure that it hasn't been generated before and then just display each number in the array.
    Second method is that you could generate a random number, store the number in an array and display it, making sure once again that the number hasn't been used before.
    If you had a lot of numbers it may take a while before you found the last few numbers.
    The Last method would be to create an array and fill it with numbers in sequence (eg. 1-5 or 1-...) then randomly sort the array. You would never have a duplicate number and it would be the fastest way.

    I think your
    CONST MaxFileNames= 5;
    is a good idea, but you need to use it.
    It should be implemented anywhere where you would have the number 5.
    eg:
    [code]
    VAR a: Array[1..MaxFileNames] of Integer;
    b: Array[1..MaxFileNames] of Integer;
    i, x, runs: Integer;
    [/code]

    Also, to make Gaashius' code work properly, you can change this:
    [code]
    {...}
    repeat
    x:=random(MaxFileNames)+1;
    if memo[x]=false then begin
    {...}
    [/code]
    to this:
    [code]
    {...}
    repeat
    [b] repeat [/b]
    x:=random(MaxFileNames)+1;
    [b] until memo[x]=false;[/b]
    if memo[x]=false then begin
    {...}
    [/code]

    Hope this helps

    Phat Nat

  • GaashiusGaashius Posts: 349Member
    : : : : : : [b][red]This message was edited by NinthAngle at 2005-9-15 6:51:55[/red][/b][hr]
    : : : : : : Ive been trying to create code that will randomly create a number and only display that number if it hasnt been shown before. This it does 5 times.
    :
    : : : [code]
    : : : var
    : : : x,runs:byte;
    : : : memo:array[1..5] of boolean;
    : : :
    : : : begin
    : : : for x:=1 to 5 do memo[x]:=false;
    : : : repeat
    : : : x:=random(5)+1;
    : : : if memo[x]=false then begin
    : : : memo[x]:=true; writeln(x);
    : : : end;
    : : : inc(runs);
    : : : until runs=5;
    : : : end;
    : : : [/code]
    : : : Now it should work.
    : : : ****************
    : : : Any questions? Just ask!
    : : :
    : : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : : :
    : : :
    : : :
    : :
    : : hi,
    : : im not sure i understand this code. whenever I execute thge program, I simply get the thee numbers 2, 5 and 1. everytime. shouldnt the random(x) command be responsible for giving a different set of numbers every time. shouldnt that same set be 5 and not 3 numbers???
    :
    : First off, if you don't put RANDOMIZE in your code (preferrably at the start, only needed once) you will get the same results everytime. Gaashius' code takes a random number and if it has been shown before it counts that as a run.
    : There are a few ways that you could do it.
    : You could generate an array of random numbers, making sure that it hasn't been generated before and then just display each number in the array.
    : Second method is that you could generate a random number, store the number in an array and display it, making sure once again that the number hasn't been used before.
    : If you had a lot of numbers it may take a while before you found the last few numbers.
    : The Last method would be to create an array and fill it with numbers in sequence (eg. 1-5 or 1-...) then randomly sort the array. You would never have a duplicate number and it would be the fastest way.
    :
    : I think your
    : CONST MaxFileNames= 5;
    : is a good idea, but you need to use it.
    : It should be implemented anywhere where you would have the number 5.
    : eg:
    : [code]
    : VAR a: Array[1..MaxFileNames] of Integer;
    : b: Array[1..MaxFileNames] of Integer;
    : i, x, runs: Integer;
    : [/code]
    :
    : Also, to make Gaashius' code work properly, you can change this:
    : [code]
    : {...}
    : repeat
    : x:=random(MaxFileNames)+1;
    : if memo[x]=false then begin
    : {...}
    : [/code]
    : to this:
    : [code]
    : {...}
    : repeat
    : [b] repeat [/b]
    : x:=random(MaxFileNames)+1;
    : [b] until memo[x]=false;[/b]
    : if memo[x]=false then begin
    : {...}
    : [/code]
    :
    : Hope this helps
    :
    : Phat Nat
    :
    :
    You are right.... sorry again :-((you know I just didn't test the codes - next time I will).
    ****************
    Any questions? Just ask!

    :-) [b][blue]GAASHIUS[/blue][/b] :-)


  • zibadianzibadian Posts: 6,349Member
    : : : : : : : [b][red]This message was edited by NinthAngle at 2005-9-15 6:51:55[/red][/b][hr]
    : : : : : : : Ive been trying to create code that will randomly create a number and only display that number if it hasnt been shown before. This it does 5 times.
    : :
    : : : : [code]
    : : : : var
    : : : : x,runs:byte;
    : : : : memo:array[1..5] of boolean;
    : : : :
    : : : : begin
    : : : : for x:=1 to 5 do memo[x]:=false;
    : : : : repeat
    : : : : x:=random(5)+1;
    : : : : if memo[x]=false then begin
    : : : : memo[x]:=true; writeln(x);
    : : : : end;
    : : : : inc(runs);
    : : : : until runs=5;
    : : : : end;
    : : : : [/code]
    : : : : Now it should work.
    : : : : ****************
    : : : : Any questions? Just ask!
    : : : :
    : : : : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    : : : :
    : : : :
    : : : :
    : : :
    : : : hi,
    : : : im not sure i understand this code. whenever I execute thge program, I simply get the thee numbers 2, 5 and 1. everytime. shouldnt the random(x) command be responsible for giving a different set of numbers every time. shouldnt that same set be 5 and not 3 numbers???
    : :
    : : First off, if you don't put RANDOMIZE in your code (preferrably at the start, only needed once) you will get the same results everytime. Gaashius' code takes a random number and if it has been shown before it counts that as a run.
    : : There are a few ways that you could do it.
    : : You could generate an array of random numbers, making sure that it hasn't been generated before and then just display each number in the array.
    : : Second method is that you could generate a random number, store the number in an array and display it, making sure once again that the number hasn't been used before.
    : : If you had a lot of numbers it may take a while before you found the last few numbers.
    : : The Last method would be to create an array and fill it with numbers in sequence (eg. 1-5 or 1-...) then randomly sort the array. You would never have a duplicate number and it would be the fastest way.
    : :
    : : I think your
    : : CONST MaxFileNames= 5;
    : : is a good idea, but you need to use it.
    : : It should be implemented anywhere where you would have the number 5.
    : : eg:
    : : [code]
    : : VAR a: Array[1..MaxFileNames] of Integer;
    : : b: Array[1..MaxFileNames] of Integer;
    : : i, x, runs: Integer;
    : : [/code]
    : :
    : : Also, to make Gaashius' code work properly, you can change this:
    : : [code]
    : : {...}
    : : repeat
    : : x:=random(MaxFileNames)+1;
    : : if memo[x]=false then begin
    : : {...}
    : : [/code]
    : : to this:
    : : [code]
    : : {...}
    : : repeat
    : : [b] repeat [/b]
    : : x:=random(MaxFileNames)+1;
    : : [b] until memo[x]=false;[/b]
    : : if memo[x]=false then begin
    : : {...}
    : : [/code]
    : :
    : : Hope this helps
    : :
    : : Phat Nat
    : :
    : :
    : You are right.... sorry again :-((you know I just didn't test the codes - next time I will).
    : ****************
    : Any questions? Just ask!
    :
    : :-) [b][blue]GAASHIUS[/blue][/b] :-)
    :
    :
    :
    Here is a somewhat faster alternative:
    [code]
    VAR
    a: Array[1..MaxFileNames] of Integer;
    i, j: integer;
    temp: integer;
    begin
    for i := 1 to MaxFileNames do
    a[i] := i;
    for i := 1 to MaxFileNames do
    for j := 1 to MaxFileNames+1 do
    if Random() < 0.5 then
    begin
    Temp := a[j];
    a[j] := a[j+1];
    a[j+1] := Temp;
    end;
    end;
    [/code]
    This code is especially faster when MaxFileNames increases, because the inner repeat-until loop will take increasingly as the array gets filled.
Sign In or Register to comment.