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 5 11322
"lallous" <la*****@lgwm.o rg> 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
"David B. Held" <dh***@codelogi cconsulting.com > wrote in message
news:bn******** **@news.astound .net... "lallous" <la*****@lgwm.o rg> 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 SearchForZeroAn dSize(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/
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/
> 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.merke rk(at)dse.nl
"Rob Williscroft" <rt*@freenet.RE MOVE.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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
indices for that container.
So I need to generate 3 unique numbers from integer range .
There seems to be no simple and efficient way to do this. Any
implementation I have come up with involves maintaining a list of
|
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?
STL set, map?
Could you give me a little code example?
Thank you.
|
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 p_reference = ref_prefix || SUBSTR(CAST(reference AS CHAR(12)),3);
|
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 numbers using asp.net?
Plus If you have any coding practice ideas for the above defined project
please share them.
Thanks!
|
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 10,7,8,6,9,4,5,2,3,1.
Can anyone provide me with the code?
Thanks,
| |
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 using rand() method to print unique random number.
And finally i want to know, If a program generate random numbers and the same program will execute after 100 or some days, it will not to generate the old random number (i.e the random number...
|
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 numbers. ie., out of 1,2,3 and 4 only two numbers should be picked randomly and those two numbers should not be repeated. Any help please....
Regards,
VRK
|
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 of the Python session.
I can't use the id() of the object, because that is only guaranteed to be
unique during the lifetime of the object.
For my application, it doesn't matter if the ids are predictable, so I
could do something as simple as...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |