C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old January 14th, 2003, 08:34 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
Random numbers every millisecond?

The following code creates a random number by "srand"ing with a value from "time" which only changes every second. This means the random numbers are the same if the program is run twice during the same second. How can I change this to be MORE random more often (every millisecond or smaller)?

Quote:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void main()
{
unsigned int number;
time_t t;

srand((unsigned)time(&t));
number = rand() % 100;
printf("%d", number);
}

Reply With Quote
  #2  
Old January 15th, 2003, 04:01 PM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
Matthew,

If you are using a UNIX system, you can get your randomness from one of the random devices that most systems have now.

If you need to generate a string of random numbers for your application, you would be better served to write your program for such a purpose.

For example:

Code:
int count;
int i;

srand(time(0));

printf("How many numbers? ");
sscanf("%d", &count);

for(i=0; i < count; i++) {
  printf("%d", rand() % 100);
  if (i % 5) printf("\t");
  else printf("\n");
}
__________________
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/

Reply With Quote
  #3  
Old January 16th, 2003, 12:45 AM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 594 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 6 m 15 sec
Reputation Power: 21
To explain ClayD's code a bit more.

rand() is realy just a list of numbers.

srand() determines where in the list to start.

In your example srand() is called every time you want a random number. This resets the 'list' to the same position if the time has not changed ie called in same second. ClayD's will only call it once so the list will be incremented after each call to rand() .

Note: rand() is not random. It has an bias of around ten percent.

If ClayD's solution is not random enough then run a search on google for more random number generators.
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

Frank Zappa

Reply With Quote
  #4  
Old January 16th, 2003, 11:02 AM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
I understand how rand() and srand() work. I just need a value that changes more often than every second to pass into srand(). If I had one that changed every 1000th of a second, that would do it.

Reply With Quote
  #5  
Old January 16th, 2003, 11:32 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
if you used srand for every number then you would need a clock that changed very quickly, but cd's code doesn't call srand for every number.

cd's code only srands once. then all of the numbers are generated onwards from that first srand number position.

that's how i see it anyway, so you don't need a quick changing clock.




srand and seeding - same thing right?

Reply With Quote
  #6  
Old January 16th, 2003, 12:05 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
I do not want to use srand for each number, just for each execute of my script, in which the possibility exists that it can be run twice within one second.

srand uses a seed to start a random sequence, so I think srand and seeding is the same thing.

Reply With Quote
  #7  
Old January 16th, 2003, 12:42 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
you didn't make that clear that you wanted more than one lot of random numbers - that that's what the problem was.

it's still the same issue though - you just make sure you only seed the number once. that's what the problem is here - seeding at the same time, or not.

so put this round the srand call within your random function to make sure srand gets called only once:

Code:
static int x=0;
if(x==0) {
	srand(time(0));
	x=1;
}


static sustains the x value, in whichever function or block that it is used. so it'll stay at 1 even when the programme leaves that block.

Reply With Quote
  #8  
Old January 16th, 2003, 12:58 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,430 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 21 h 41 m 55 sec
Reputation Power: 784
Since you're using *nix, why don't you try reading from /dev/random or /dev/urandom. Something like this:
Code:
#include <stdio.h>
int main(void) {
  FILE *fp;
  unsigned char c1;

  if ((fp = fopen("/dev/urandom", "r")) == NULL) {
    fprintf(stderr, "Error! Could not open /dev/urandom for read\n");
    return -1;
  }

  c1 = fgetc(fp);
  fclose(fp);

  printf("%d\n", c1);
  return 0;
}


Note that the code above reads a single char at a time, so the rand value will be between 0 and 255. If you want larger values, read more bytes from it!

Reply With Quote
  #9  
Old January 16th, 2003, 09:51 PM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 594 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 6 m 15 sec
Reputation Power: 21
You could try GetTickCount() for a millisec timer (with no accuracy)

Reply With Quote
  #10  
Old January 17th, 2003, 09:44 AM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
Quote:
Originally posted by balance
you didn't make that clear that you wanted more than one lot of random numbers - that that's what the problem was.


In my first post I said, "This means the random numbers are the same if the program is run twice during the same second." So I thought that covered it. Sorry for being unclear!

Reply With Quote
  #11  
Old January 17th, 2003, 09:46 AM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
balance,

I'm not sure if you still understand what I meant. I only want to use srand once during the program. But I want the seed passed into srand to be different every time, even if the program is run twice within the same second.

Reply With Quote
  #12  
Old January 17th, 2003, 09:47 AM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
Quote:
Originally posted by Scorpions4ever
Since you're using *nix, why don't you try reading from /dev/random or /dev/urandom.


Cool. What is /dev/random anyway? How is it filled?

Reply With Quote
  #13  
Old January 17th, 2003, 09:54 AM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
Quote:
Originally posted by TechNoFear
You could try GetTickCount() for a millisec timer (with no accuracy)


What unit defines GetTickCount()?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Random numbers every millisecond?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |