Avatar of inversojvo
inversojvoFlag for Germany

asked on 

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

Avatar of undefined
Last Comment
pgnatyuk
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Blurred text
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.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Blurred text
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
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Blurred text
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.
Avatar of inversojvo
inversojvo
Flag of Germany image

ASKER

random() did not work with C.
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> 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.
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

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
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

What you say now?
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

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

Avatar of Infinity08
Infinity08
Flag of Belgium image

>> 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 ?
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> 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.
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

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?

Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

>>That you've proved my point
I've proved my point.
:)
Avatar of Infinity08
Infinity08
Flag of Belgium image

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;'.
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

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

Ok. No problem at all. Have a nice week.
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> 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.
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

You are right. Thanks.

C
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
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo