Hi. I'm fairly new to programming, and I'm trying to make a rock paper scissors simulator (text based). One problem I have, however, is that my compiler repeatedly tells me that a 'void value not ignored as it ought to be' prevents me from running my program. Here's the snippet that is"hates":

float placeholder;

placeholder = srand( (unsigned)time(NULL) );

Can someone please explain to me what I'm doing wrong, and how to fix it?

P.S. I apologize for how idiotic and brainless I may sound. :)

Thanks.

Recommended Answers

All 6 Replies

srand() returns void. You are trying to assign that void return to a float. No, no!

srand( (unsigned)time(NULL) );

That's all you need before you call rand()

srand() doesn't return a value. All that function does is seed the random number generator.

What you want is rand(), not srand(). srand() should be called only once at the beginning of the program to seed the generator. From then on the program calls rand() to get a random number.

#include <time.h>

int main()
{
   srand( time(0) );

   int n = rand();
}

Note that rand() returns an integer, not a float or double.

what they ^, ^^ said above.

but just to add some background information to put it in context for you:

the 's' in "srand" the stands for "seed"... meaning you have to first "seed" the randomization function before you can start generating random numbers.

this has to do with the fact that nothing is truly random, and these so-called random numbers are actually pseudo-random, generated by a mathematical algorithm that gives the appearance of being random.

the algorithm requires a "seed" value to start, and "srand" is the function that generates this pseudo-random seed.


.

rand generates an integer but you need a float random number.

Since nobody bothered to show you how to get a random floating-point value, here's a complete program to help you out:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ( void )
{
    int i;

    srand ( (unsigned)time ( NULL ) );

    for ( i = 0; i < 10; i++ ) {
        double r = rand() * ( 1.0 / ( RAND_MAX + 1.0 ) );

        printf ( "%f\t%f\t%f\n", r, r * 10, r * (20 - 10) + 10 );
    }

    return 0;
}

Originally, the value of r is a pseudorandom floating-point value in the range of [0,1). This is represented by the first column of output.

The second column multiplies that value by some whole number, which produces a floating-point value in the range of [0,N) where N is the multiplier.

The third column takes it another step and gives the range a lower bound that isn't zero. Multiplying by the difference between the upper and lower bounds and then adding the lower bound, you can get a floating-point value in the range of [M,N) where M is the lower bound (10 in this case) and N is the upper bound (20 in this case).

I'd also be remiss if I didn't mention that using the result of the time function as an argument to srand isn't guaranteed to work, though you aren't likely to find a system where it fails.

Thanks guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.