programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# How do I generate a Random numbers using C?

Ranch Hand
Posts: 123
• Number of slices to send:
Optional 'thank-you' note:
my problem here is how do I generate a random number instead of just letting someone typing the number to show the result.

here is a code that I'm trying to figure out how to implement the random numbers generator.

author
Posts: 23949
142
• Number of slices to send:
Optional 'thank-you' note:

The C library has a rand() function to generate pseudo random numbers. It should work for both Unix and Windows.

Henry

jin Otanashi
Ranch Hand
Posts: 123
• Number of slices to send:
Optional 'thank-you' note:

Henry Wong wrote:
The C library has a rand() function to generate pseudo random numbers. It should work for both Unix and Windows.

Henry

trying to do that with this part but is does not work

Perhaps i need a example or something that show rand () work using the scanf (...&)

Marshal
Posts: 79066
376
• Number of slices to send:
Optional 'thank-you' note:
Good grief! Have you really been told to use eight spaces, because your indentation is really difficult to read. And separate out your code a bit. Not
for(i=0;i<n;i++) // line 2 of second block
but
for (i = 0; i < n; i++)
Can you really use scanf with a number as its argument? I am sure that is wrong.>

Henry Wong
author
Posts: 23949
142
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:
Can you really use scanf with a number as its argument? I am sure that is wrong.

Correct. This is definitely wrong.  With C/C++, just like Java, the result of the assignment is the value of the assignment. This means that the value of the expression, which includes a random component, is being passed to the scanf() function. Depending on what is passed, this call will likely seg fault.

Henry

jin Otanashi
Ranch Hand
Posts: 123
• Number of slices to send:
Optional 'thank-you' note:

Henry Wong wrote:

Campbell Ritchie wrote:
Can you really use scanf with a number as its argument? I am sure that is wrong.

Correct. This is definitely wrong.  With C/C++, just like Java, the result of the assignment is the value of the assignment. This means that the value of the expression, which includes a random component, is being passed to the scanf() function. Depending on what is passed, this call will likely seg fault.

Henry

Sorry about that I have never use the random numbers generator in C and i thought that scanf will input a random numbers in scanf then output a random numbers ( random page sequences size enter like 1 2 3 4 5 1 1 1 in the array) when i keep clicking run each time this will output a different numbers in each array.

 Don't get me started about those stupid light bulbs.