How to choose numbers random with C?

inversojvo
inversojvo used Ask the Experts™
on
Hi,

A number between 1 and 15 should be choosen random in C. The choice will be repeated 15 times in one game.

It should also be possible to repeat the game a few times without repeating a sequence.

In C I just know int rand(void). But I do not see the possibility to limit the numbers to be choosen from 1 to 15.

I also do not see the possibility to avoid the repetition of the numbers sequence for a few times.

Could somebody help me?

Many thanks and have a nice day!

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009
Commented:
>> In C I just know int rand(void).

That's a good start.

>> But I do not see the possibility to limit the numbers to be choosen from 1 to 15.

rand returns a number between 0 and RAND_MAX. If you want to limit the range to a pre-specified one, there's an easy way to do that. You can generate a random number between x and y (x inclusive) like this :

        x + (rand() % (y - x))

so, for values from 1 .. 15 :

        1 + (rand() % 15)

>> I also do not see the possibility to avoid the repetition of the numbers sequence for a few times.

If you don't want repetition, you'll have to keep track of the values that have already been generated, and if one is generated again, just generate another one.
Commented:
Seed the random number generator:

srand(time(NULL));

Then:

int num = random() % 15 + 1

num will be between 1 and 15.

I attached a function that returns "random" numbers between a and b.

int Random(int a, int b)
{
	int result = random() % a + b;
	return result;
}

Open in new window

Commented:
C++ : Reference : C Library : cstdlib (stdlib.h) : rand:
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

Author

Commented:
random() did not work with C.
Top Expert 2009

Commented:
>> random() did not work with C.

Because it's not a standard function. That was probably a typo.

Note however that the Random function posted earlier will not work as advertised. So be careful with that.

Commented:
gcc-4.2.1

Here is the code.
And here is the output:
Running…
i = 1, random = 15
i = 2, random = 15
i = 3, random = 15
i = 4, random = 15
i = 5, random = 15
i = 6, random = 15
i = 7, random = 15
i = 8, random = 15
i = 9, random = 15


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

int Random(int a, int b)
{
	int result = random() % a + b;
	return result;
}

int main () 
{
	int i;
	srand(time(NULL));
	
	for (i = 1; i < 10; ++i) {
		printf("i = %d, random = %d\n", i, Random(1, 15));
	}
    return 0;
}

Open in new window

Terminal---bash---113-34.jpg

Commented:
What you say now?

Commented:
Output:
i = 1, random = 14
i = 2, random = 2
i = 3, random = 13
i = 4, random = 11
i = 5, random = 9
i = 6, random = 11
i = 7, random = 2
i = 8, random = 13
i = 9, random = 10

There was a bug, but it was no about random() :)

int Random(int a, int b)
{
	int result = random() % b + a;
	return result;
}

Open in new window

Top Expert 2009

Commented:
>> What you say now?

Euhm. That you've proved my point. Do you consider a random number generator that always generates the number 15, even though it should be generating numbers between 1 and 15, as correct behavior ?
Top Expert 2009

Commented:
>> There was a bug, but it was no about random() :

Now try passing a lower limit other than 1 as the first parameter, and see if it still behaves correctly ;)

Btw, random is not a standard function. Just because your platform implements it doesn't mean that others do.

Commented:
The question was not about the rand(). Right? inversojvo mentioned the correct name in the question.
Anyway, in the function was a bug.

http://www.gnu.org/s/libc/manual/html_node/ISO-Random.html

It's not only about me. For example, here is about Borland:
http://wakish.info/random-numbers-in-c-programming/

It can be even so: http://www.cs.unibo.it/~montreso/doc/C/corso2/section2_22_26.html

random() is "a kind of an improvement" :) - BSD random number function:
http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_17.html#SEC296

the prototype is in stdlib.h

Does it really matter?

Commented:
>>That you've proved my point
I've proved my point.
:)
Top Expert 2009

Commented:
pgnatyuk, calm down ;) Nobody's after you, and this is not about random vs. rand ... (I only mentioned it because the author said it didn't work for him - and I wanted to clarify why it didn't work).

The problem with the function is that it doesn't do what it's supposed to.

You posted the output of the original version in http:#32828268, which is clearly wrong, hence proving my point (and the cause of my post http:#32828283).
The corrected version is still wrong however (see my post http:#32828287). You need to use the formula I posted in the very first post, not just 'random() % b + a;'.

Commented:
How you can say that if your see the code and the output.

Ok. No problem at all. Have a nice week.
Top Expert 2009

Commented:
>> How you can say that if your see the code and the output.

Just try calling your function like this a few times :

        Random(16, 30)

and see what numbers it returns. They will not be between 16 and 30 as you might expect. They will be between 16 and 45.

Commented:
You are right. Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial