473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Random number generation

Hi

I am generating 256 bit key for AES. I am using the following implementation

Expand|Select|Wrap|Line Numbers
  1. void fill_random_key(char *key)
  2. {
  3.     /* Length of the key */
  4.     unsigned short int length = KEYLENGTH;
  5.  
  6.     /* Seed number for rand() */
  7.     srand((unsigned int) time(0) + getpid());
  8.     int i=0;
  9.  
  10.     while(length--) {
  11.         key[i]=rand() % 127;
  12.         srand(rand());
  13.  
  14.         i++;
  15.  
  16.     }
  17. }
  18.  
  19. void get_keys_for_files(char **files)
  20.  {
  21.     extern int num_of_files;
  22.     char (*curr_file)[20]=files;
  23.  
  24.     char key[32];
  25.     int i=0;
  26.     FILE *fp;
  27.     char key_file[1024];
  28.     for(i=0;i<num_of_files;i++)
  29.      {
  30.  
  31.         strcpy(key_file,cwd);
  32.         strcat(key_file,"\\keys\\");
  33.         strcat(key_file,curr_file);
  34.         fill_random_key(key);
  35.         fp=fopen(key_file,"w");
  36.         fputs(key,fp);
  37.         curr_file++;
  38.         fclose(fp);
  39.         sleep(1000);
  40.      }
  41.  }
  42.  
  43.  
The problem with this code is that if I remove sleep it will give same keys for almost all files. But I do not want to deter performance of my module due to this. Can somebody let me know better implementation? rand_s() is not available. I am working on code blocks : mingw.


Thanks for your help
Jan 29 '12 #1

✓ answered by Rabbit

That's because you're seeding with the same number otherwise. You need to move on to the next number in the sequence.

4 1174
Rabbit
12,516 Expert Mod 8TB
That's because you're seeding with the same number otherwise. You need to move on to the next number in the sequence.
Jan 29 '12 #2
Yes I am looking for a method in which I can generate randon numbers without using sleep. (with sleep i can pass different seed) but i am looking for an alternative to sleep so that performance do not deter
Jan 30 '12 #3
Rabbit
12,516 Expert Mod 8TB
I'm saying that you're seeding with the same number each time it's called. That's why you're getting the same key. The solution is simple, just seed once, or include your loop value in the seed. You don't need sleep and you don't need an alternative to sleep. Also, you don't need to reseed everytime to get the next character.
Jan 30 '12 #4
import java.util.Random;


public class RandomGen {

public static void main(String[] args) {

Random r=new Random();
System.out.println(r.nextInt(1001) + r.nextInt(9000));
}
}
Jan 31 '12 #5

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

Similar topics

5
by: drs | last post by:
Is there any way to generate random numbers based on arbitrary real valued functions? I am looking for something like random.gauss() but with natural log and exponential functions. thanks, -d
4
by: mescaline | last post by:
hi, i'm new to C++ could anyone refer me to a good site / good examples of random numbers? in particular including: 1) the commnds to obtain normally and exponenetially distributed r...
10
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of...
4
by: Dimos | last post by:
Hello All, I need some help with random number generation. What I need exactly is: To create a few thousand numbers, decimal and integers, between 5 and 90, and then to export them as a...
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
8
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0;...
6
by: Fan924 | last post by:
How do I add random number generation to this background image slide show? Everything I try kills if. TIA _____________________________________ <html> <head> <meta http-equiv="Content-Type"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.