473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Generating unique random numbers

Hello,

This code works fine when 'size' is less than 32768 however when size is
bigger this function never returns.
Can't find out why?!
If I break into the code I can see that 'i' is 32768....
void MakeRandomArray(unsigned long **a, unsigned long size)
{
unsigned long *data = new unsigned long [size];
double sizef = (double)(size - 1);
char *map = new char[size];
unsigned long n, i;

memset(data, 0, size * sizeof(unsigned long));
memset(map, 0, size * sizeof(char));

srand((unsigned) time(NULL));

for (i=0;i<size;i++)
{
for (;;)
{
double f = ((double) rand()) / RAND_MAX;
n = (unsigned long)(f * sizef);
if (map[n])
continue;
data[i] = n;
map[n] = 1;
break;
}
}
delete [] map;
*a = data;
}

--
Regards,
Elias
Jul 19 '05 #1
5 11286
"lallous" <la*****@lgwm.org> wrote in message
news:bn************@ID-161723.news.uni-berlin.de...

This code works fine when 'size' is less than 32768 however
when size is bigger this function never returns.
Can't find out why?!
It would seem that the most likely problem is here:
[...]
double f = ((double) rand()) / RAND_MAX;
n = (unsigned long)(f * sizef);
if (map[n])
continue;
[...]


Perhaps your RNG doesn't give sufficient resolution after
floating-point conversions to cover your domain. One way
to test this is to write a loop which tries to obtain each
number from 0 to sizef, and maybe displays the number of
attempts to get it. That way, you get a better idea of the
coverage your RNG is providing.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
Jul 19 '05 #2
"David B. Held" <dh***@codelogicconsulting.com> wrote in message
news:bn**********@news.astound.net...
"lallous" <la*****@lgwm.org> wrote in message
news:bn************@ID-161723.news.uni-berlin.de...

This code works fine when 'size' is less than 32768 however
when size is bigger this function never returns.
Can't find out why?!


It would seem that the most likely problem is here:
[...]
double f = ((double) rand()) / RAND_MAX;
n = (unsigned long)(f * sizef);
if (map[n])
continue;
[...]


Perhaps your RNG doesn't give sufficient resolution after
floating-point conversions to cover your domain. One way
to test this is to write a loop which tries to obtain each
number from 0 to sizef, and maybe displays the number of
attempts to get it. That way, you get a better idea of the
coverage your RNG is providing.

Dave

Wrote some function:
void SearchForZeroAndSize(unsigned long size)
{
double sizef = (double)(size);
unsigned long n;

srand((unsigned)time(NULL));

unsigned long ntries(0);
bool nozero = true, nosize = true;

while (nozero || nosize)
{
double f = ((double) rand()) / RAND_MAX;
f = f * sizef;
n = (unsigned long)(f);
if (n==0 && nozero)
{
printf("found zero after %ld tries\n", ntries);
nozero = false;
}
else if (n==size && nosize)
{
printf("found 'size' after %ld tries\n", ntries);
nosize = false;
}
ntries++;
}
}

Output:
found 'size' after 9902 tries
found zero after 44910 tries

Here is some more info:

unsigned long i, biggest = 0;
for (i=0; i < size; i++)
{
for (;;)
{
bool flag = false;
double f = ((double) rand()) / RAND_MAX;
f = f * sizef;
n = (unsigned long)(f);

if (n > biggest)
{
biggest = n;
if (biggest == 99999)
n = biggest; // useless code, but just to put a breakpoint
printf("biggest so far: %ld\n", biggest);
}

if (flag) // when the function hangs, put a BP here and adjust flag to
TRUE
{
unsigned long filled(0), unfilled(0);
for (unsigned long j=0;j<size;j++)
{
if (!map[j])
{
unfilled++;
//printf("%d is not filled!\n", j);
}
else
filled++;
if (j % 1000 == 0)
{
//printf("filled: %ld unfilled:%ld\n", filled, unfilled);
}
}
printf("filled: %ld unfilled:%ld\n", filled, unfilled);
}
if (map[n])
continue;
data[i] = n;
map[n] = 1;
break;
}
}

The output goes:

biggest so far: 97978
biggest so far: 98790
biggest so far: 99510
biggest so far: 99718
biggest so far: 99916
biggest so far: 99995
biggest so far: 99999
filled: 32768 unfilled:67232

This asserts that I am getting random numbers above 32768...

p.s: I am using VC6++

--
Elias
http://lgwm.org/
Jul 19 '05 #3
lallous wrote in news:bn************@ID-161723.news.uni-berlin.de:
Hello,

This code works fine when 'size' is less than 32768 however when size is bigger this function never returns.
Can't find out why?!
Because 32768 is RAND_MAX on your platform so at some point
your map ends up with RAND_MAX entries set to 1 and your inner
loop never will get passed "if (map[n]) continue;" as n is always
one of the previously set values.

Note that the scaling you do with sizef distributes every value
in [0, RAND_MAX] to a distinct value in [0, size - 1], so when
size > RAND_MAX there are simply some values that will never
appear.
void MakeRandomArray(unsigned long **a, unsigned long size)
{
unsigned long *data = new unsigned long [size];
double sizef = (double)(size - 1);
char *map = new char[size];
unsigned long n, i;

memset(data, 0, size * sizeof(unsigned long));
memset(map, 0, size * sizeof(char));

srand((unsigned) time(NULL));

for (i=0;i<size;i++)
{
for (;;)
{
double f = ((double) rand()) / RAND_MAX;
n = (unsigned long)(f * sizef);
if (map[n])
continue;
data[i] = n;
map[n] = 1;
break;
}
}
delete [] map;
*a = data;
}


Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #4
> This code works fine when 'size' is less than 32768 however when size
is
bigger this function never returns.
Can't find out why?!
If I break into the code I can see that 'i' is 32768....
void MakeRandomArray(unsigned long **a, unsigned long size)
{
unsigned long *data = new unsigned long [size];
double sizef = (double)(size - 1);
char *map = new char[size];
unsigned long n, i;

memset(data, 0, size * sizeof(unsigned long));
memset(map, 0, size * sizeof(char));

srand((unsigned) time(NULL));

for (i=0;i<size;i++)
{
for (;;)
{
double f = ((double) rand()) / RAND_MAX;
n = (unsigned long)(f * sizef);
if (map[n])
continue;
data[i] = n;
map[n] = 1;
break;
}
}
delete [] map;
*a = data;
}


Hmm, look more like C than C++ code to me.

Anyway the most likely reason why this function never returns is that
the rand() function only generates 32768 unique numbers (size >
RAND_MAX), so after 32768 unique numbers have been generated this
function can no longer find new unique numbers. You probably want to use
a different random generator
(http://en.wikipedia.org/wiki/Mersenne_Twister), as the standard ones
are often not particulary good.

Your algorithm is not particulary efficient. A faster way would be to
fill a vector with unique numbers and then shuffle it:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> MakeRandomArray(int size)
{
vector<int> v(size);

for(int i = 0; i < v.size(); ++i)
{
v[i] = i;
}

random_shuffle(v.begin(), v.end());
return v;
}

int main()
{
cout << "Enter size: ";
int size;
cin >> size;
vector<int> v = MakeRandomArray(size);

for(int i = 0; i < v.size(); ++i)
{
cout << v[i] <<" ";
}
return 0;
}

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 19 '05 #5
"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
lallous wrote in news:bn************@ID-161723.news.uni-berlin.de:
Hello,

This code works fine when 'size' is less than 32768 however when size

is
bigger this function never returns.
Can't find out why?!


Because 32768 is RAND_MAX on your platform so at some point
your map ends up with RAND_MAX entries set to 1 and your inner
loop never will get passed "if (map[n]) continue;" as n is always
one of the previously set values.

Note that the scaling you do with sizef distributes every value
in [0, RAND_MAX] to a distinct value in [0, size - 1], so when
size > RAND_MAX there are simply some values that will never
appear.


Thanks Rob, that makes sense.
Also thanks to Peter, STL and random_shuffle looks nice.

--
Elias
http://lgwm.org/
Jul 19 '05 #6

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

Similar topics

5
by: Ross MacGregor | last post by:
I have a very simple yet complicated problem. I want to generate a random list of indices (int's) for a container. Let's say I have a container with 10 items and I want a list of 3 random...
7
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? ...
6
by: Poul Møller Hansen | last post by:
I have made a stored procedure, containing this part for generating a unique reference number. SET i = 0; REPEAT SET i = i + 1; SELECT RAND() INTO reference FROM SYSIBM.SYSDUMMY1; SET...
6
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
1
by: Wally | last post by:
I am after VB.net code for a random number generator that will generate 10 different numbers between 1 and 10 and put them into an array. For example I want the output to be something like...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
6
by: vrkamalakar | last post by:
Hi, I need a piece of code to generate a random number between 0 and 5 (both exclusive) ie., number should be either 1,2,3 or 4. And also I need only two unique random numbers out of the four...
14
by: Steven D'Aprano | last post by:
I have an application that will be producing many instances, using them for a while, then tossing them away, and I want each one to have a unique identifier that won't be re-used for the lifetime...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.