473,498 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to generate a random generator in C?

1 New Member
I am trying to use the default random generator in C.
the funcion rand() is fine but when tried 'srand(time(0))' it is giving me the following error:


error C3861: 'time': identifier not found




the code I am tryin it in is as follows:

#include <stdio.h>
#include <stdafx.h>
#include <iostream>
#include <stdlib.h>


int main(void)
{
int i, m, x[100];

printf("\n\nEnter the length of the array: ");
scanf_s("%d", &m);

for(i=0; i<m; i++) x[i]=0;

for(i=0; i<m; i++) x[i]=srand(time(0));

printf("\n\n");
for(i=0; i<m; i++) printf(" %d", x[i]);
printf("\n\n");
}
Oct 3 '08 #1
4 9391
JosAH
11,448 Recognized Expert MVP
You have to include another header file: <time.h> if you want to use the time() function.

kind regards,

Jos
Oct 3 '08 #2
Tassos Souris
152 New Member
Let me tell you one interesting thing about srand

When you call srand again with the same seed value then the exact same sequence of pseudo-random integers in range 0 to RAND_MAX shall be repeated.

I think this may help you in debugging and see what is the behavior of your program under the same conditions.

With respect,

Tassos Souris
Oct 6 '08 #3
sicarie
4,677 Recognized Expert Moderator Specialist
Small notes on style:

If you are using C, you can remove the declaration of the C++ header 'iostream'. That may cause issues later on.

Also, best practice is considered to use 'int main()' and then show a return value at the end of your program that will declare if the program completed successfully or not.

Did using Jos' selection of including time.h fix the error?
Oct 6 '08 #4
donbock
2,426 Recognized Expert Top Contributor
1. I suggest the argument to time() be NULL rather than 0.
2. I suggest that you explicitly cast the time() return value to unsigned int.

See below. I have repeated the prototypes for the library functions for your reference, but you should accomplish the same by including the appropriate header files.
Expand|Select|Wrap|Line Numbers
  1. /* FYI: prototypes for the standard library functions */
  2. void srand(unsigned int seed);
  3. time_t time(time_t *timer);
  4.  
  5. srand( (unsigned int)time(NULL) );
Oct 6 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
8271
by: Laphan | last post by:
Hi All This is a strange request, but I just cannot fathom how to do it. In theory the requirement is very basic, but in practise its a noodle!! I have 10 team names like so: Team A Team...
4
2376
by: Wahoo | last post by:
Another question,my teacher gave me a code for generate a random number from 1 - range, but I can't made it work, where is the problem? Thanks!!
5
16560
by: Yudan YI \(OSU\) | last post by:
I want to generate random data from a normal distribution, while I checked the functions, and I found rand(), which returns a pseudorandom integer between zero and RAND_MAX. I am not sure how to...
18
2620
by: Toby Newman | last post by:
I need to randomly choose one of four paths in my program. Using the tools I know, the best way I can think to do it is by doing something like the following: //==============================...
2
18000
by: Henry | last post by:
Hi, How can I generate an eight digit random? Can I use the staff name to generate it? May I ask is there any sample c# code to see? Thanks
12
5195
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
4
2419
by: fAnSKyer | last post by:
When using random we can get a uniform data sample. But how to transfer this sample in to a Guassion Sample? Thanks? Did C or C++ provide any function to do this? Thanks a lot. Cheers....
20
7800
by: jjmillertime | last post by:
I'm new so i apologize if this is in the wrong spot. I'm also new to programming in C and i've been searching for quite a while on how to create a program using C that will generate two random...
9
6584
by: Chelong | last post by:
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include<iostream> #include <time.h> int main() {
24
7173
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.