473,466 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Number generator problem...

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::PowerGen() {
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[i]);
Console::WriteLine(S"Writing new...");
allNum[i] = temp;
i++;
}
}

return allNum;
}

bool PowerGen::contains(int* n, int y, int size)
{

for (int i = 0; i < size; i++) {
if (n[i] == y) {
return true;
}
}
return false;
}

PowerGen::~PowerGen() {
}[/code:1:bb235d45c8]

Thanks,
Jerry
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 17 '05 #1
2 1765
"jerryau" <je*******@yahoo.com-dot-au.no-spam.invalid> wrote in message
news:40**********@Usenet.com...
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: [...] Thanks,
Jerry


You should not forget to seed the random number generator (srand), before
generating any random number using rand().

srand( (unsigned)time( NULL ) );

Don't forget that you must be careful that each number has an equal chance
of appearing. The following generates a random integer between 0 and n-1,
with the chance of each number appearing ~ (1 / n).

int rand_n(int n)
{
if(n<=0 || n>RAND_MAX)
return -1;
int bucket_size = RAND_MAX / n;
int r;
do
r = rand() / bucket_size;
while( r >= n );
return r;
}

You can then perhaps make a random array class.

// random numbers from 1..rand_max
class RandArray
{
int rand_max;
std::vector<int> random_numbers;
public:
RandArray(int size, int rand_max_)
: random_numbers(size), rand_max(0)
{ Regenerate(rand_max_); }

int GetSize() const { return random_numbers.size(); }
int GetRandMax() const { return rand_max; }
int operator[](int i) const { return random_numbers[i]; }

void Regenerate(int rand_max_=-1)
{
if(rand_max_>= 0)
rand_max = rand_max_;
for(int i=0; i!=size; ++i)
random_numbers[i] = 1 + rand_n(rand_max);
}
};

If you need to have new random numbers for a new game, call Regenerate on
the RandomArray object.

Tom.
Nov 17 '05 #2
"TT (Tom Tempelaere)" <_N**************@hotmail.comMAPSO_N_> wrote in
message news:2Z*********************@phobos.telenet-ops.be...
"jerryau" <je*******@yahoo.com-dot-au.no-spam.invalid> wrote in message
news:40**********@Usenet.com...
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

Sorry, I missed the word unique. The implementation of RandArray::Regenerate
would be totally different. I would suggest using random_shuffle from
<algorithm>.

class RandArray
{
std::vector<int> random_numbers;
public:
RandArray(int size)
{
random_numbers.reserve(size);
for(int i=0; i!=size; ++i)
random_numbers.push_back( i+1 );
Regenerate();
}
void Regenerate()
{
std::random_shuffle(random_numbers.begin(), random_numbers.end());
}
int GetSize() const { return random_numbers.size(); }
int operator[](int i) const { return random_numbers[i]; }
};
[...]
Thanks,
Jerry


Tom.
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Aaron | last post by:
I need some help writing this function the function returns a list of random non-duplicate integers in a string, 1 parameter indicating the maximum range. string randGen(int maxRange) ...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
4
by: Wahoo | last post by:
Another question,my teacher gave me a code for generate a random number from 1 - range, but I can't made it work, where is the problem? Thanks!!
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
11
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
40
by: RadiationX | last post by:
I have a problem that I really don't understand at all. In my previous post I could get started on my projects I just had a few problems with syntax errors. This problem is something that I don't...
73
by: cesco | last post by:
I have to generate a list of N random numbers (integer) whose sum is equal to M. If, for example, I have to generate 5 random numbers whose sum is 50 a possible solution could be . Is there a...
8
by: remlostime | last post by:
i use g++ to generater rand number, now i find that the RAND_MAX is 32367 in my computer, how can i make a bigger rand number( the number is wihin in the integer(2^32-1))
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.