Re: random data in structure - checking for no double values
- From: Duncan Muirhead <noone@xxxxxxxxxxxx>
- Date: Thu, 22 Jun 2006 12:09:30 +0100
On Thu, 22 Jun 2006 01:00:45 -0700, Erich Pul wrote:
hi!I think this is off topic for this news group. However I'd suggest
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("\nGezogen wurden folgende Zahlen: %d %d %d %d %d %d",
ziehungT.z1,ziehungT.z2,ziehungT.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,
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(int 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)rand()/(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
.
- Follow-Ups:
- Re: random data in structure - checking for no double values
- From: Erich Pul
- Re: random data in structure - checking for no double values
- References:
- random data in structure - checking for no double values
- From: Erich Pul
- random data in structure - checking for no double values
- Prev by Date: Re: random data in structure - checking for no double values
- Next by Date: Re: Template Engine like Smarty
- Previous by thread: Re: random data in structure - checking for no double values
- Next by thread: Re: random data in structure - checking for no double values
- Index(es):
Relevant Pages
|
Loading