Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,391 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,134 people online right now. Registration is fast and FREE... Join Now!




generating random numbers in C

 
Reply to this topicStart new topic

generating random numbers in C, problem with while loop

proaspirer
18 Sep, 2006 - 07:03 PM
Post #1

New D.I.C Head
*

Joined: 29 Jul, 2006
Posts: 5


My Contributions
guys my code is smethin like this....

CODE

# include <stdio.h>
# include <conio.h>

void main()
{
int a,m,i,x[100];
int k=1,count=0;
float R[150];
clrscr();
printf("Enter a\n");
scanf("%d",&a);
printf("\nEnter m\n");
scanf("%d",&m);
for(i=0;i<4;i++)
{
x[1]=0;
printf("Enter value of x[0]");
scanf("%d",&x[i]);
do
{
x[k]=(a*x[k-1])%m;
R[k]=(float)x[k]/m;
count++;
printf(" %d\t %0.2f\n",x[k],R[k]);
}while(x[k]!=x[i])
k++;
printf("%d",count);
}
getch();
}

i want the random numbers to come for x[0]=1,2,3,4
and the while loop is givin me a problem...pls help
User is offlineProfile CardPM
+Quote Post

Xing
RE: Generating Random Numbers In C
18 Sep, 2006 - 07:17 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Look up srand and time functions.
User is offlineProfile CardPM
+Quote Post

proaspirer
RE: Generating Random Numbers In C
19 Sep, 2006 - 05:08 AM
Post #3

New D.I.C Head
*

Joined: 29 Jul, 2006
Posts: 5


My Contributions
QUOTE(Xing @ 18 Sep, 2006 - 08:17 PM) *

Look up srand and time functions.



Its an assignment n i cannot use inbulit functions.....generate random numbers thru my program only...
User is offlineProfile CardPM
+Quote Post

Xing
RE: Generating Random Numbers In C
19 Sep, 2006 - 05:57 AM
Post #4

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Pseudorandom number generator
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Generating Random Numbers In C
19 Sep, 2006 - 06:41 AM
Post #5

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
What you have done is not random at all.
Here's a hint.

Take the number of milliseconds that have passed since midnight at different intervals of time and perform operations on this number so that it works out to be in the range that you require.
User is offlineProfile CardPM
+Quote Post

proaspirer
RE: Generating Random Numbers In C
20 Sep, 2006 - 03:54 AM
Post #6

New D.I.C Head
*

Joined: 29 Jul, 2006
Posts: 5


My Contributions
QUOTE(Xing @ 19 Sep, 2006 - 06:57 AM) *




hey can u help me wth lzw encoding...whch i had posted yesterday???
User is offlineProfile CardPM
+Quote Post

Xing
RE: Generating Random Numbers In C
20 Sep, 2006 - 04:55 AM
Post #7

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(proaspirer @ 20 Sep, 2006 - 05:24 PM) *
hey can u help me wth lzw encoding...whch i had posted yesterday???

Only when you format it properly with small comments.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:30AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month