I have an array of integer of size 20 and there values range from 0-9 and i want atleast the value 0 placed in one location in the array once. how can i control rand() to be able to do that? cause there a possibility 1/10 will randomily generate 0

#include<stdio.h>
#include<stdlib.h>
main()
{

int array[20], i;

for(i=0;i<20;i++)
array[i] = rand()%10;

return 0;
}

Well if you can control the output of a random function then it wont remain a RANDOM function right ??? :D
Well you can always do this.First assume a variable "a" which is defined as follows :

1>a=Some random number from 1 to 19 (%20) generated by some random number generating method.
2>Place 0 in this block of array as array[a]=0.
3>Now your are sure that the array has one zero and also in a random place generated by computer itself.
4>Now go on with your for loop as above itself and just skip the loop when i==a.

And ya you have loads of threads here at daniweb to make the random function better.Just have a look at these threads it may help.

Comments
if i tell you the secret, it won't be a secret!

Load 19 random values. If a 0 is generated during these 19 values, remember that fact.

For the 20th value, if a 0 was not generated, load a 0, otherwise get one more random number.

>For the 20th value, if a 0 was not generated, load
>a 0, otherwise get one more random number.

So if 0 was not generated, it'll always be the 20th value? I'd suggest a final step of randomly shuffling the values. That way you get the required 0, but it's not in a predictable location.

>I'd suggest a final step of randomly shuffling the values.
That can complicate things. I would suggest OP that: after assigning values 0 to 9 to all those 20 location, choose a location randomly and assign value zero to it.

>That can complicate things.
Duh. It is an extra step, after all. :icon_rolleyes: On the plus side it adds another level of randomness.

>Duh. It is an extra step, after all.
You mean you can do the entire re-shuffling in one step(in two or more statements?) ? Enlighten me how?

>Enlighten me how?
Step 1: Fill the array using rand.
Step 2: Check for zero and if not present, add it to the end.
Step 3: Randomly shuffle the array.

Apparently you have an extremely limited idea of what a step is.

>in two or more statements?
I can do it in one statement, seeing as how a loop is a compound statement and the shuffle can be done with a single loop. See? I can play games with terminology too. ;)

Comments
Nice play ;)

>Step 1: Fill the array using rand.
>Step 2: Check for zero and if not present, add it to the end.
This is already being discussed.
>Step 3: Randomly shuffle the array.
Code it baby, code it. As I can say "Build an operating system." is a single step


>Apparently you have an extremely limited idea of what a step is.
Yeah. My recent interaction with low level languages are responsible for this.


>See? I can play games with terminology too.
I am very glad to know this.:yawn:

>As I can say "Build an operating system." is a single step
It is, but you have to increase the granularity of your steps to come up with reasonable milestones. I find it somewhat humorous that you compare building an operating system to a trivial twenty line homework program.

>My recent interaction with low level languages are responsible for this.
That's a bad thing. Even at the lowest level you still need to keep an abstract view of your design.

A day ago csurfer's algorithm solved the problem. That's "pipelined" version:

int i, j = rand()%20;
    for (i = 0; i < 20; i++)
        array[i] = (i != j)*(rand()%9 + 1);

In addition: it's impossible to add another level of randomness by random shuffles or other tricks while we are using the same (extremely bad ;)) rand() generator.

>I find it somewhat humorous that you compare building an operating system to a
>trivial twenty line homework program.
Then my purpose was solved.

>That's a bad thing. Even at the lowest level you still need to keep an abstract
>view of your design.
The program I was interacting with were very trivial ( prime number generator ). It was in a algorithm book. There, the author used a ASM, to demonstrate that what a high-level program assumes to be `a step' is actually a series of steps in machine code.
But you said it right, one should be able to `quantize' a step. It also varies from person to person and to which domain he is working.

>it's impossible to add another level of randomness by random shuffles or other
>tricks while we are using the same (extremely bad ) rand() generator.
She assumed a perfect random generator while making that remark, IMHO. And she was right too. More random calls will tend to dilate the variance. (of course, I am too, making this remark assuming a perfect random generator) :)

>^ so.... whatever happened to June 6th?
The plan stretched. Didn't bother to change the signature. (I know this is ignorance). Long story!!...

>More random calls will tend to dilate the variance.
The variance of... what? We didn't estimate random sequence parameters in that case so the variance (or other moments) does not bear a relation to the sequence of 20 random numbers.

After filling the array make a loop to check whether there is any element with zero value
If not then fill it again if it is then break

This question has already been answered. Start a new discussion instead.