# random interger command problem

Page 1 of 1

## 4 Replies - 3991 Views - Last Post: 06 April 2008 - 09:47 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=48339&amp;s=f8135f69e2c662669d92be418fbaa7eb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Tavisha

Reputation: 1
• Posts: 42
• Joined: 28-February 08

# random interger command problem

Posted 06 April 2008 - 03:56 PM

Hi All

I wanted to know what is the best function which i can use in my code to generate a random integer between 1 and 10,000.
I m working on a channel coding algorithm and want to put a command which can generate any random number between 1 and 10,000.
The way I hav tried is : it is in BOLD.

```#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<iostream.h>
#include<fstream.h>

int main()

{

FILE *f1, *f2;
float eprob;
int r,num=0;
float x;
float r_num;
char a,b='0', c ='1';

f1= fopen("rcpcoutput.txt","r");
f2=fopen( "erroutput.txt","w");
printf("\n Enter the error probability");
scanf("%f",&eprob);
printf("\n Error probability is %1f",eprob);

while (!feof(f1))

{
fscanf(f1,"%c",&a);
[b] r=(10000);[/b]
printf("random is %d",r);
x=(float)r;

r_num=x/10000;
if (r_num>eprob)
{
if (a==b)
a=c;
else a=b;
num++;
}
fprintf (f2,"%c",a);
printf("random is %f probabilty is %f written is %c",r_num,eprob,a);

}
printf("\n\n Number of bit errors is %d",num);
fclose(f1);
fclose(f2);
getch();
}

```

Please help me in solve it. The results jus reads my input bits as such. how can I get any random number ?
can some one help me learn the basics of generating a random number in C code?

Is This A Good Question/Topic? 0

## Replies To: random interger command problem

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: random interger command problem

Posted 06 April 2008 - 04:33 PM

### #3 Tavisha

Reputation: 1
• Posts: 42
• Joined: 28-February 08

## Re: random interger command problem

Posted 06 April 2008 - 05:00 PM

Hey Thanks so much!

I think i cud figure it out..

just one little help Ineed....

I used it to generate a random number between 0 and 10... like this :

r= rand () % 10 +1

and I get the right results...

so what should I right for generating a random num between 1 and 10,000 ???

shud it be :

r= rand() % 10000 +1

its a lil confusing even now ??

Reputation:

## Re: random interger command problem

Posted 06 April 2008 - 08:56 PM

Yes, change to 10 to 10000 and it should work fine.

r= rand() % 10000+1;

This will actually create a random number from 0 to 9999.

The lowest number it will produce would be zero. The highest would be 9999. So when you put the +1 at the end of the statement, it adds that to whatever number it generates, making your range from 1 to 10000.
If you changed your base number to 5...

r= rand() % 10000+5; You would get a number between 5 and 10004
r= rand() % 10000+10; You would get a number between 10 and 10009
r= rand() % 10000+50; You would get a number between 50 and 10049

Take that sniplet Amadeus sent you, and change the numbers around, and compile it a few times changing the values each time, you'll get it easily.

@Amadeus - tsk tsk tsk... you used a sniplet from another website?
Hold out you hand while I find my ruler.....

This post has been edited by Whizzy: 06 April 2008 - 09:27 PM

### #5 Tavisha

Reputation: 1
• Posts: 42
• Joined: 28-February 08

## Re: random interger command problem

Posted 06 April 2008 - 09:47 PM

hey guys
thanks so much for all the help...solved out my prob realquik.....

Man.... Ilove this siteeee..... I am hope-ing that very soon I 'll b able to help ppl to )