i'm tying 2 describe my problem here.plz solve it for me..

1. generate a random number b2in 0 and 1 (r);
2. do t=r*2.5 ;
3. do w= t/100
4. keep track of all t1,t2,t3.... in an array and do the sum S=(t1+t2+t3......) ;
5. repeat steps 1,2,3,4, till S<100
6. store the corresponding w1,w2,w3... in another array/ heap
7. sort them in ascending order.

Recommended Answers

All 12 Replies

its not a place that we can solve your homework problem.

still take some hints and show ur effort , the code you have tried so far.Then only you can expect something from this forum.

1)rand() function is used to generate random numbers.

int y=rand();
int num=(y-1)/y;

then you proceed with your next steps...

Thanks,
DP

its not a place that we can solve your homework problem.

still take some hints and show ur effort , the code you have tried so far.Then only you can expect something from this forum.

1)rand() function is used to generate random numbers.

int y=rand();
int num=(y-1)/y;

then you proceed with your next steps...

Thanks,
DP

Num will be always 0 :D

Num will be always 0 :D

Hey sory i forgot about type casting as i was giving a dummy code.

int y=rand();int num=(y-1)/y;int y=rand();
float num=(float) (y-1)/y;

it should be type cased to float.
Thanks,
DP

i tried but everytime it shows 0.00000
and no other value

i tried it another way....
with the srand() function
but all it gives between 0 and 1 is 0
but what i want is 0.1 or 0.2... something like that
how can i get it?
and it's notlike i haven't work with the algo
just i couldn't

i tried but everytime it shows 0.00000
and no other value

i tried it another way....
with the srand() function
but all it gives between 0 and 1 is 0
but what i want is 0.1 or 0.2... something like that
how can i get it?
and it's notlike i haven't work with the algo
just i couldn't

try like this...

int y=rand();  //y should be an integer
float num=(float)(y-1)/y; //num should be a float and its typecasted
printf("%d\t%f",y,num);

if you want to know more about random number generators
goto:

http://www.eternallyconfuzzled.com/arts/jsw_art_rand.aspx

> i tried but everytime it shows 0.00000
> and no other value
> i tried it another way....
Look, just post some actual code, and describe what doesn't work in that particular program.

Don't just post "I tried x y and z, and it didn't work".
We can't help you fix your mistakes if you never show us any code to begin with.

if i want 2 create 40 random numbers
logically it should be done by a for loop
i tried as follows
but it shows 1 number.
i can't understand my faults

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

int random(void)
{
	return(1+rand()%3);
}

int main(void)
{
	int i;
	srand(time(0));
    for(i=0;i<40;i++);
	printf("the number is %d\n",random());
}

i tried to the algo as follows
i've changed the algo a bit
1.r=1+rand()%3;
2.t=r*1.25;
3.do w= t/100;
4. keep track of all t1,t2,t3.... in an array /* upto this is ok*/
and do the sum S=(t1+t2+t3......) ;/*from this step i can't do*/
5. repeat steps 1,2,3,4, till S<100
6. store the corresponding w1,w2,w3... in another array/ heap
7. sort them in ascending order.

for(i=0;i<40;i++); /* extraneous semicolon */

First up, your code for generating 40 random numbers will not work as you have a semi-colon after your for statement. That's why you only see the one number being output. (David Sinkula has already pointed this out - sorry didn't realise this when writing this message).

Now, the reason you think the same number is being output is due to your statement:

return(1+rand()%3);

Order of precedence rules will dictate that the % operator will be calculated first up and you will end up with an intermediate result being either 0, 1, or 2. When you add 1 to that result, your range of possibilities are obviously 1, 2 or 3. Why you think you keep seeing 1 as the output probably depends on how many times you ran your program. I tried your code out and after only a few runs I got all 3 expected results of either 1, 2 or 3.

Looking back at your original post, you are looking to generate random numbers between 0 and 1 (not sure on whether the bounds are inclusive or not). Assuming you're looking to generate random numbers between [0,1), try the following code snippet out:

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

int main(void) {

    double rand_num;
    int i;

    srand(time(NULL));

    for (i = 0; i < 40; ++i) {
        rand_num = (double)rand() / (RAND_MAX + 1);
        printf("Random #%d is %f\n", i, rand_num);
    }

    return 0;
}

The above code should satisfy your initial requirement for generating random numbers between 0 and 1. (Note: the constant RAND_MAX is defined in the stdlib.h header file - it is the maximum number that can be returned by the rand() function).

Hopefully from there you will be able to continue on with the rest of your algorithm.

Cheers,
JD

PS: When posting code, please use code tags.

i tried to the algo as follows
i've changed the algo a bit
1.r=1+rand()%3;
2.t=r*1.25;
3.do w= t/100;
4. keep track of all t1,t2,t3.... in an array /* upto this is ok*/
and do the sum S=(t1+t2+t3......) ;/*from this step i can't do*/
5. repeat steps 1,2,3,4, till S<100
6. store the corresponding w1,w2,w3... in another array/ heap
7. sort them in ascending order.

Don't post your algo agian...just work as per the suggestions given.
If u put some effort definately u will be able to write your own program.
In fact this thread consists of a lot of informaions about random number generation.
Thanks,
DP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.