# How to choose numbers random with C?

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!

C

Last Comment
pgnatyuk
Infinity08

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
pgnatyuk

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
pgnatyuk

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
inversojvo

random() did not work with C.
Infinity08

>> 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.
pgnatyuk

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;
}
``````
Terminal---bash---113-34.jpg
pgnatyuk

What you say now?
pgnatyuk

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;
}
``````
Infinity08

>> 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 ?
Infinity08

>> 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.
pgnatyuk

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

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?

pgnatyuk

>>That you've proved my point
I've proved my point.
:)
Infinity08

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;'.
pgnatyuk

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

Ok. No problem at all. Have a nice week.
Infinity08

>> 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.
pgnatyuk

You are right. Thanks.

C

C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, so it has found lasting use in applications that had formerly been coded in assembly language, including operating systems as well as various application software for computers ranging from supercomputers to embedded systems. It is distinct from C++ (which has its roots in C) and C#, and many later languages have borrowed directly or indirectly from C.

23K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts

TRUSTED BY