The randomize()

Randomize() should randomize random(x) function. But it doesn't in the following code. Without randomize() the program gives the same every time you execute it. With randomize() it prints all with nothing inside for about 6 clicks, then all solid, about 6 clicks, and so on. What's wrong with randomize()?

The code:
[code]
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unitbook.h"
#include <stdlib.h>
#include <math.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void swap(bool& l, bool& r)
{
bool temp = l;
l = r;
r = temp;
}

void reverse(bool arr[], size_t n)
{
bool* end = arr + n;
while(arr < --end)
swap(*(arr++), *end);
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
Memo1->Text="";
bool a[5];
int c=0;
int c1=0;
char lf=10,cr=13;
while(c<6)
{
a[c]=random(2);
c++;
}
reverse(a,5);
while(c1<6)
{
if(a[c1]==0)
{
Memo1->Text=Memo1->Text+"———"+cr+lf;
}
else
{
Memo1->Text=Memo1->Text+"— —"+cr+lf;
}
c1++;
}
}
//---------------------------------------------------------------------------
Last edited on
One problem is that you access memory you don't own.
1
2
3
4
5
6
bool a[5]; 
while(c<6)
{
a[c]=random(2);
c++;
}
Valid indexes are from 0 to 4.
Topic archived. No new replies allowed.