473,401 Members | 2,125 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,401 software developers and data experts.

Random number generation in array

Hi everybody!
Here's the problem:
I have an array of 4 integer elements and they must be initialized by random
numbers from 0 to 9 that shouldn't repeat. I tried many things, but some
elements (for example, first and third ones) continue repeating. Can you
propose an algorithm that solves this?
Thank you!
Jul 22 '05 #1
5 2208
Artyom wrote:
Hi everybody!
Here's the problem:
I have an array of 4 integer elements and they must be initialized by random
numbers from 0 to 9 that shouldn't repeat. I tried many things, but some
elements (for example, first and third ones) continue repeating. Can you
propose an algorithm that solves this?
Thank you!


std::set<int> numbers;
while (numbers.size() <= 4) {
int random_int = get_random_integer();
numbers.insert(random_int);
}

// move values from set into array

Jul 22 '05 #2

"Artyom" <ar****@i.com.ua> wrote in message
news:c0***********@news.dg.net.ua...
Hi everybody!
Here's the problem:
I have an array of 4 integer elements and they must be initialized by random numbers from 0 to 9 that shouldn't repeat. I tried many things, but some
elements (for example, first and third ones) continue repeating. Can you
propose an algorithm that solves this?
Thank you!


Fill an array (size 10) with numbers 0 to 9

Go though array randomly swapping each element with a later element (a
random shuffle)

Pick first four elements from randomly shuffled array.

john
Jul 22 '05 #3
lilburne wrote:
Artyom wrote:
Hi everybody!
Here's the problem:
I have an array of 4 integer elements and they must be initialized by random
numbers from 0 to 9 that shouldn't repeat. I tried many things, but some
elements (for example, first and third ones) continue repeating. Can you
propose an algorithm that solves this?
Thank you!


std::set<int> numbers;
while (numbers.size() <= 4) {
int random_int = get_random_integer();
numbers.insert(random_int);
}


This could loop forever. What if get_random_integer() returns every time
the same number? It's still 'random'!

<http://www.sgi.com/tech/stl/random_shuffle.html> gives a nice STL solution
to the problem.

--
Miika Karanki /1\
mk******@iki.fi / 0 \ - "It's more fun to compute"
http://www.iki.fi/mkaranki/ / 1 \ Kraftwerk

Jul 22 '05 #4
Miika Karanki <mk******@iki.fi> kirjoitti 7 Feb 2004 22:57:05 GMT:
lilburne wrote:
Artyom wrote:
Hi everybody!
Here's the problem:
I have an array of 4 integer elements and they must be initialized by random
numbers from 0 to 9 that shouldn't repeat. I tried many things, but some
elements (for example, first and third ones) continue repeating. Can you
propose an algorithm that solves this?
Thank you!
<http://www.sgi.com/tech/stl/random_shuffle.html> gives a nice STL solution
to the problem.


Sorry, it doesnt. ;)
--
Miika Karanki /1\
mk******@iki.fi / 0 \ - "It's more fun to compute"
http://www.iki.fi/mkaranki/ / 1 \ Kraftwerk

Jul 22 '05 #5
On 7 Feb 2004 23:09:32 GMT in comp.lang.c++, Miika Karanki
<mk******@iki.fi> was alleged to have written:
<http://www.sgi.com/tech/stl/random_shuffle.html> gives a nice STL solution
to the problem.


Sorry, it doesnt. ;)


What's not to like about it?

int nums[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::random_shuffle(nums, nums+10);
std::copy(nums, nums+4, std::ostream_iterator<int>(std::cout, " "));
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
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...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
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?
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
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;...
16
by: jason.cipriani | last post by:
I am looking for a random number generator implementation with the following requirements: - Thread-safe, re-entrant. - Produces consistently reproducible sequences of psuedo-random numbers...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.