random interger command problem

How to put a command in my code which can read any random integr b/w 1

Page 1 of 1

4 Replies - 3398 Views - Last Post: 06 April 2008 - 09:47 PM Rate Topic: -----

#1 Tavisha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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);
		printf("\n read is %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

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: random interger command problem

Posted 06 April 2008 - 04:33 PM

http://www.cplusplus...tdlib/rand.html
Was This Post Helpful? 0
  • +
  • -

#3 Tavisha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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 ??

pleaseeee helpp......
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Whizzy*


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.....
:D

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

Was This Post Helpful? 0

#5 Tavisha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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 :o)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1