473,587 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

random data in structure - checking for no double values

hi!

i got a structure, which should be filled with random integer values
(it is in fact a generator for numbers like in a lotto), but these
values must not be recurring, so no double occurrences are desired.

my code:

<code>
Tipp* ziehung() // Tipp* is the function
{
Node *bewK; // a node that is my index
Tipp ziehungT; // a new struct for storing the random values
int x;

x = 0;
srand(time(NULL )); // init for the rand()
while(x<=20) // for testing, gimme 20 values
{
ziehungT.z1 = (rand() % 45) +1; // data input in the
struct's members
ziehungT.z2 = (rand() % 45) +1;
ziehungT.z3 = (rand() % 45) +1;
ziehungT.z4 = (rand() % 45) +1;
ziehungT.z5 = (rand() % 45) +1;
ziehungT.z6 = (rand() % 45) +1;

printf("\nGezog en wurden folgende Zahlen: %d %d %d %d %d %d",
ziehungT.z1,zie hungT.z2,ziehun gT.z3,ziehungT. z4,ziehungT.z5, ziehungT.z6);
x++;
}
}

</code>

is there a possibility to circumvent packing an array with the values
and running through several loops?

TIA,
--
EP

Jun 22 '06 #1
19 4210
"Erich Pul" <er*******@blac kbox.net> wrote:
i got a structure, which should be filled with random integer values
(it is in fact a generator for numbers like in a lotto), but these
values must not be recurring, so no double occurrences are desired.

my code:

<code>
Tipp* ziehung() // Tipp* is the function
No, ziehung() is the function. Tipp * is the type of its return value.
ziehungT.z1 = (rand() % 45) +1; // data input in the
struct's members
ziehungT.z2 = (rand() % 45) +1;
ziehungT.z3 = (rand() % 45) +1;
ziehungT.z4 = (rand() % 45) +1;
ziehungT.z5 = (rand() % 45) +1;
ziehungT.z6 = (rand() % 45) +1;
So that won't work, then.
is there a possibility to circumvent packing an array with the values
and running through several loops?


No. Is there any problem with filling an array of 45 elements with 1-45,
shuffling it (takes only a single loop of at most 44 iterations - in
this case you need only 6) and then taking the first 6 elements of the
shuffled array?

Richard
Jun 22 '06 #2
> > Tipp* ziehung() // Tipp* is the function

No, ziehung() is the function. Tipp * is the type of its return value.
yep, i meant that ; )

[...]
ziehungT.z4 = (rand() % 45) +1;
ziehungT.z5 = (rand() % 45) +1;
ziehungT.z6 = (rand() % 45) +1;


So that won't work, then.


nah, that works perfectly - the problem is that there *MAY* be values
that occur twice or more (sometimes they do, sometimes not)

No. Is there any problem with filling an array of 45 elements with 1-45,
shuffling it (takes only a single loop of at most 44 iterations - in
this case you need only 6) and then taking the first 6 elements of the
shuffled array?


basically not, but that is just a variation of the same problem,
because there is no proof that there won't be values that occur twice.

greetings,
--
Erich Pul

Jun 22 '06 #3

Erich Pul wrote:

Don't snip attributions (who said what). This was by Richard Bos:
No. Is there any problem with filling an array of 45 elements with 1-45,
shuffling it (takes only a single loop of at most 44 iterations - in
this case you need only 6) and then taking the first 6 elements of the
shuffled array?


basically not, but that is just a variation of the same problem,
because there is no proof that there won't be values that occur twice.


I don't think you read Richard's suggestion carefully. It is guaranteed
to give all different numbers. E.g.:

0 1 2 3 4 5 6 7 8 9

<shuffle>

1 7 3 5 6 2 4 9 8 0

<take first N>

Probably the easiest way to shuffle is to swap N pairs of randomly
chosen elements.

Jun 22 '06 #4

Vladimir Oka schrieb:

I don't think you read Richard's suggestion carefully. It is guaranteed
to give all different numbers. E.g.:

0 1 2 3 4 5 6 7 8 9

<shuffle>

1 7 3 5 6 2 4 9 8 0

<take first N>

Probably the easiest way to shuffle is to swap N pairs of randomly
chosen elements.


well, you are right - sorry - must use two eyes from now on. thanks,
i'll try that.

E

Jun 22 '06 #5
On Thu, 22 Jun 2006 01:00:45 -0700, Erich Pul wrote:
hi!

i got a structure, which should be filled with random integer values
(it is in fact a generator for numbers like in a lotto), but these
values must not be recurring, so no double occurrences are desired.

my code:

<code>
Tipp* ziehung() // Tipp* is the function
{
Node *bewK; // a node that is my index
Tipp ziehungT; // a new struct for storing the random values
int x;

x = 0;
srand(time(NULL )); // init for the rand()
while(x<=20) // for testing, gimme 20 values
{
ziehungT.z1 = (rand() % 45) +1; // data input in the
struct's members
ziehungT.z2 = (rand() % 45) +1;
ziehungT.z3 = (rand() % 45) +1;
ziehungT.z4 = (rand() % 45) +1;
ziehungT.z5 = (rand() % 45) +1;
ziehungT.z6 = (rand() % 45) +1;

printf("\nGezog en wurden folgende Zahlen: %d %d %d %d %d %d",
ziehungT.z1,zie hungT.z2,ziehun gT.z3,ziehungT. z4,ziehungT.z5, ziehungT.z6);
x++;
}
}

</code>

is there a possibility to circumvent packing an array with the values
and running through several loops?

TIA,

I think this is off topic for this news group. However I'd suggest
creating an array holding 1..45, shuffling it (search for Knuth
Shuffling algorithm) and then taking the last 6 entries from the array.
For example:
void KnuthShuffle(in t n, int* deck)
{ int r;
while( --n>0)
{ /* r = "random" number between 0 and n inclusive */
/* swap deck[n] and deck[r] */
}
}
If speed is very important, then you could just go round the loop 6 times,
because the last 6 entries won't change after that.

By the way, generating a random integer between 1 and 45 is better (if
somewhat more slowly) done by
1 + 45*((double)ran d()/(RAND_MAX+1.0)) than by rand() % 45) +1.
The latter will be more sensitive to the low bits being highly
correlated between calls to rand().
Duncan

Jun 22 '06 #6

Duncan Muirhead schrieb:
I think this is off topic for this news group. However I'd suggest
uhm.. why?
creating an array holding 1..45, shuffling it (search for Knuth
Shuffling algorithm) and then taking the last 6 entries from the array.
For example:
void KnuthShuffle(in t n, int* deck)
{ int r;
while( --n>0)
{ /* r = "random" number between 0 and n inclusive */
/* swap deck[n] and deck[r] */
}
}
If speed is very important, then you could just go round the loop 6 times,
because the last 6 entries won't change after that.

By the way, generating a random integer between 1 and 45 is better (if
somewhat more slowly) done by
1 + 45*((double)ran d()/(RAND_MAX+1.0)) than by rand() % 45) +1.
The latter will be more sensitive to the low bits being highly
correlated between calls to rand().
Duncan


thank you, i will focus on that for my program

E

Jun 22 '06 #7

Erich Pul wrote:
Duncan Muirhead schrieb:
I think this is off topic for this news group. However I'd suggest


uhm.. why?


Because c.l.c deals in Standard C language issues only, not general
algorithm questions (that's what comp.programmin g is for). If the
question tingles the imagination sufficiently, or is easily dealt with
you can expect to get answers, though (but don't try to abuse this).

Jun 22 '06 #8

Vladimir Oka schrieb:
Because c.l.c deals in Standard C language issues only, not general
algorithm questions (that's what comp.programmin g is for). If the
question tingles the imagination sufficiently, or is easily dealt with
you can expect to get answers, though (but don't try to abuse this).


ok, maybe i should have mentioned that i'm currently learning C, so if
i make a post here, expect the volume of my question to be quite...
unpredictable.. . : )

but thx anyway for the replies

E

Jun 22 '06 #9

Richard Heathfield wrote:
Vladimir Oka said:
Probably the easiest way to shuffle is to swap N pairs of randomly
chosen elements.


I chose N = 6, and ran 1,000,000 trials. Using your method, I got a standard
deviation of 9.63, whereas by swapping N pairs of elements chosen randomly
between i and N, where i was the loop control, I got a standard deviation
of 9.15.


I said "easiest" not "best".

(I also did not mean "N" to be the number of random numbers the OP
wanted.)

Jun 22 '06 #10

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

Similar topics

7
14646
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text file looks like this: --------
6
1332
by: Marcus Kwok | last post by:
I am in the process of converting some legacy code (written in C) to C++. The original data structure is used to hold a table of data (Gaussian distribution, some of you call it a "normal distribution"). There are two values of relevance: the "z" value (telling how many standard deviations away from the mean), and the "phi" value...
2
5434
by: Ronny Mandal | last post by:
Is there a function that will do this task properly? -- Thanks Ronny Mandal
13
3605
by: Jon Agiato | last post by:
Hello, I am sure this problem is easy to spot but I have been at this project all day and the frustration has finally overcome me. I am using this function in order to produce a standard normal distribution random number generator and then using the function in another part of my program in order to use the value. This function is called...
5
3336
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 double value in the range could come up with equal probability). I'd also like to be able to seed this generator (e.g., via the clock) so that the...
2
2344
by: James Radke | last post by:
Hello, I have a vb.net windows application that is calling an older DLL provided by a third party. They supplied a VB 6 application that, when run on my systemn successfully passes data to the database. The problem I am having is that when I pass the data from VB.NET, the fields defined as double all contain strange values. For example,...
4
1344
by: Ricardo | last post by:
Hi wizards, I have an object , this object generate a set of 9 random numbers. When I generate for example 10 object his 9 random numbers are equals. How can I make this 9 numbers are different for each object I use this code in Sub New : Dim RandomNumber As Integer Dim RandomClass As New Random
1
5182
by: ssims | last post by:
I've got this code for a random GDI+ chart generator, and the demo works, but when I try to run it on my local machine I get a compiler error: Compiler Error Message: CS0117: 'Random' does not contain a definition for 'Next' Here's the code: using System; using System.Collections; using System.ComponentModel; using System.Data;
4
2665
by: knuckels23 | last post by:
Hi All, I have a Random access file which is written using VB 6.0. I need to read this file using C#. The record used in VB to write the Random access file is as follows Type AA aa1 As Integer aa2 As Integer aa3 As Integer aa4 As Integer
0
7920
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, 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...
0
8215
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, 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. ...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5394
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...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.