• C

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!

inversojvoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>> 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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pgnatyukCommented:
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

0
pgnatyukCommented:
C++ : Reference : C Library : cstdlib (stdlib.h) : rand:
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

inversojvoAuthor Commented:
random() did not work with C.
0
Infinity08Commented:
>> 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.
0
pgnatyukCommented:
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
0
pgnatyukCommented:
What you say now?
0
pgnatyukCommented:
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

0
Infinity08Commented:
>> 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 ?
0
Infinity08Commented:
>> 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.
0
pgnatyukCommented:
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?

0
pgnatyukCommented:
>>That you've proved my point
I've proved my point.
:)
0
Infinity08Commented:
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;'.
0
pgnatyukCommented:
How you can say that if your see the code and the output.

Ok. No problem at all. Have a nice week.
0
Infinity08Commented:
>> 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.
0
pgnatyukCommented:
You are right. Thanks.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.