Re: random data in structure - checking for no double values



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("\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,
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(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



.



Relevant Pages

  • Re: How to describe array elements with UML2.0?
    ... This sounds like your code generator is just a round-trip tool (headers ... structure like an integer array is provided externally though a Marking ... implementations like "int" and array structure. ...
    (comp.object)
  • Re: simple PRNG?
    ... range has the same probability (uniform distribution). ... one that works even on platforms where int is only 16 bit would be ... If float operations must be avoided, and you want a random integer ... Here, sK stands for simple KISS, one of a class I call KISS RNGs, ...
    (comp.lang.c)
  • Re: Simple Random Integer Generation
    ... I simply need a function that will generate a random integer between say 1 and 999. ... Its an instance of the RandomGen class just in case ... anyone wants to implement a different random number generator. ... its just like any other language it has to do it in the IO monad. ...
    (comp.lang.functional)
  • Better use of random number genator bits?
    ... If I have a random number generator creating random binary digits, ... and I want to construct a random integer between 0 and say 40, ... Also another way we know is to produce a 226 bit random number but the math ...
    (sci.math)
  • Re: simple PRNG?
    ... the range has the same probability (uniform distribution). ... one that works even on platforms where int is only 16 bit would ... If float operations must be avoided, and you want a random integer ... from 0 to n-1, you can mask off bits after invoking sK, so that enough ...
    (comp.lang.c)

Loading