J
jerryau
Hi,
I'm creating a number generator program, that is supposed to generate
6 unique random numbers for each game. I want to generate this for 6
games. The problem is, that it works for the first game, but then
all the other games have exactly the same numbers (e.g. Game 1:
1,2,3,4,5,6.. Game 2: 1,2.3,4,5,6). I don't know how to get delete
the previous array with those numbers, and then generate a new one.
Here is my code:
NumGenerator.cpp (Main class):
// This is the main project file for VC++
application project
// generated using an Application Wizard.
#include "stdafx.h"
#include "PowerGen.h"
#using <mscorlib.dll>
using namespace System;
int _tmain()
{
for (int i = 1; i <= 6; i++) {
Console::Write(S"Game ");
Console::Write(i);
Console::WriteLine(S":");
Console::WriteLine(S"-------");
int * pwNum = new int[];
PowerGen * pw = new PowerGen();
pwNum = pw->getNum();
for (int x = 0; x < 6; x++) {
Console::Write(pwNum[x]);
Console::Write(S", ");
}
Console::WriteLine();
Console::WriteLine();
delete pwNum;
delete pw;
}
return 0;
}
PowerGen.h:
[code:1:bb235d45c8]#using <mscorlib.dll>
__gc class PowerGen {
public:
PowerGen();
~PowerGen();
int* getNum();
private:
bool contains(int* n, int y, int size);
int* allNum;
};[/code:1:bb235d45c8]
PowerGen.cpp (This is where the random generating happens):
[code:1:bb235d45c8]#include "stdafx.h"
#include "PowerGen.h"
using namespace System;
PowerGen:owerGen() {
allNum = new int[6];
}
int* PowerGen::getNum() {
Random* r = new Random();
int i = 0;
int temp = 0;
while (i < 6) {
temp = r -> Next(1,45);
Console::Write(S"Temp");
Console::WriteLine(temp);
if (contains(allNum, temp, i)) {
temp = r -> Next(1,45);
} else {
Console::Write(S"Old: ");
Console::WriteLine(allNum);
Console::WriteLine(S"Writing new...");
allNum = temp;
i++;
}
}
return allNum;
}
bool PowerGen::contains(int* n, int y, int size)
{
for (int i = 0; i < size; i++) {
if (n == y) {
return true;
}
}
return false;
}
PowerGen::~PowerGen() {
}[/code:1:bb235d45c8]
Thanks,
Jerry
I'm creating a number generator program, that is supposed to generate
6 unique random numbers for each game. I want to generate this for 6
games. The problem is, that it works for the first game, but then
all the other games have exactly the same numbers (e.g. Game 1:
1,2,3,4,5,6.. Game 2: 1,2.3,4,5,6). I don't know how to get delete
the previous array with those numbers, and then generate a new one.
Here is my code:
NumGenerator.cpp (Main class):
// This is the main project file for VC++
application project
// generated using an Application Wizard.
#include "stdafx.h"
#include "PowerGen.h"
#using <mscorlib.dll>
using namespace System;
int _tmain()
{
for (int i = 1; i <= 6; i++) {
Console::Write(S"Game ");
Console::Write(i);
Console::WriteLine(S":");
Console::WriteLine(S"-------");
int * pwNum = new int[];
PowerGen * pw = new PowerGen();
pwNum = pw->getNum();
for (int x = 0; x < 6; x++) {
Console::Write(pwNum[x]);
Console::Write(S", ");
}
Console::WriteLine();
Console::WriteLine();
delete pwNum;
delete pw;
}
return 0;
}
PowerGen.h:
[code:1:bb235d45c8]#using <mscorlib.dll>
__gc class PowerGen {
public:
PowerGen();
~PowerGen();
int* getNum();
private:
bool contains(int* n, int y, int size);
int* allNum;
};[/code:1:bb235d45c8]
PowerGen.cpp (This is where the random generating happens):
[code:1:bb235d45c8]#include "stdafx.h"
#include "PowerGen.h"
using namespace System;
PowerGen:owerGen() {
allNum = new int[6];
}
int* PowerGen::getNum() {
Random* r = new Random();
int i = 0;
int temp = 0;
while (i < 6) {
temp = r -> Next(1,45);
Console::Write(S"Temp");
Console::WriteLine(temp);
if (contains(allNum, temp, i)) {
temp = r -> Next(1,45);
} else {
Console::Write(S"Old: ");
Console::WriteLine(allNum);
Console::WriteLine(S"Writing new...");
allNum = temp;
i++;
}
}
return allNum;
}
bool PowerGen::contains(int* n, int y, int size)
{
for (int i = 0; i < size; i++) {
if (n == y) {
return true;
}
}
return false;
}
PowerGen::~PowerGen() {
}[/code:1:bb235d45c8]
Thanks,
Jerry